📅 2016-Mar-09 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ clipboard, copy, paste, shell, vim, xclip ⬩ 📚 Archive
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
$ pwd | xclip
$ 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