xclip is a very useful tool to copy and paste text content between the shell and GUI (X11) programs. You can pipe text to xclip and that text will be copied to the X clipboard. You can invoke xclip to output and it will spit out the content in the X clipboard. Once you learn it, you will wonder how you ever worked without it at the shell, inside Vim and across SSH sessions.
$ sudo apt install xclip
- To copy output from shell to X11 clipboard:
$ pwd | xclip
- To paste from X11 clipboard to shell:
$ xclip -o | ls
- Note that xclip works with the X clipboard, not the clipboard maintained by your window manager. Yes, you have two clipboards in Linux! So, if this copy-paste is not working for you, then the selection copied to your X clipboard might not be appearing in your window manager clipboard. Typically, the window manager clipboard is configured by default for all of this to work transparently. If it is not working, then you can configure the window manager clipboard to get all the selections from X clipboard.
To copy from Vim to clipboard: Visually highlight the text or lines you want to copy and type
:w !xclip. The
:w command is used to write the entire contents of the buffer or the visually highlighted text to a specified output file. Instead of a file, we write that text to xclip here, so it will be copied to the clipboard.
To copy from clipboard to Vim: Just do the reverse of the above operation. Type this command
:r !xclip -o. The
:r command in Vim is used to read text into the current buffer. So, this command takes the output of xclip and inserts it into the currently buffer.
Copy or paste in SSH session: The above commands works without any change between your local computer and the remote computer you have SSHed into! Yes, works like magic all thanks to the X11 protocol! Note that the remote computer should have xclip installed and you should have created the SSH session with X11 forwarding.
Tried with: XClip 0.12 and Ubuntu 14.04
There are times when you want to check if you are in a SSH session from your shell. For example, I like to change the shell prompt based on whether it is in a local machine or in a SSH session on another machine.
An easy way to do this is to check if any environment variable related to SSH has been set. For example, I check if the
SSH_CLIENT variable is set in my shell. In Fish, this can be done by checking the result of
set --query SSH_CLIENT for example.
I like to have Fish as my default shell in Byobu. I add these lines to my
set -g default-shell /usr/bin/fish
set -g default-command /usr/bin/fish
Note that Byobu will set the
SHELL environment variable to the above path. That is, even if you want to set
SHELL to something else in your shell’s init file, that will be overridden by Byobu.
Tried with: Byobu 5.74 and Ubuntu 14.04
There are many operations at the shell that need a bit of looping or automation. You can learn programming in the language of the shell you use to achieve this. Since I know a bit of Python, I prefer to use it for running quick commands at the shell. The call to execute a command at the shell is
I typically use this call to automate repeated commands that I want to run at the shell. For example, I open a Python interpreter from your shell and type:
for i in range(100, 200):
s = "montage foo-" + str(i) + ".png bar-" + str(i) + ".png -tile 2x1 foobar-" + str(i) + ".png"
This quickly makes pairs from two sets of 100 images I have, puts them together and creates a new set of 100 images. Pretty sweet to automate operating on 100 images with just a few lines of code! 🙂
Tried with: Python 2.7.6 and Ubuntu 14.04
I have a Python script named foo.py. Since, I would like to run it directly from the shell, I have added a shebang to run it through Python:
print "Hello world"
If I run it directly at the shell, I get a strange error:
: No such file or directory
However, if I run it explicitly with the Python interpreter, it works fine:
$ python foo.py
This strange error turned out to be due to the newline markers in the file. The file was created on Windows and had CR+LF as the newline marker. But, I was running the script on Linux, which only uses LF as the newline marker.
So, this must have led the env program to run the character CR as a command. And obviously, it could not find any such file or directory.
The solution is simple. Convert the newline markers in the file to that of the host system, in this case to Linux. This can be done easily in Vim by using the fileformat option.
Tried with: GNU CoreUtils 8.12.197, Python 2.7.3 and Ubuntu 12.04 LTS