Home ·  Blog ·  Debian ·  FreeDict ·  software projects ·  publications ·  Impressum     

Blog posts about free software, usage, configuration and my involvement in free software projects.

Image of the word tautologie as
            LaTeX-output \tau\to\log{}ie


copy (clipboard)-text between a tty and a GUI

Posted by Sebastian Humenda at 02.04.2014, 16:35

For blind programmers using text editors like Vim, the command line is in most cases the only convinient work environment. On the other hand some tasks are not possible on a raw tty, therefore a GUI is necessary. Copying text between a tty and a X session is more or less a pain. In this short article, I'll describe xclip and some convenient ways to use it with Vim / ZSH / GNU screen.

First of all, you should install it, e.g. through using apt-get install xclip. To copy hi to your clipboard, try:

echo "hi" | xclip -d :0

You can view the contents of your clipboard using

xclip -o :0 -i

You can of course also copy files into the clipboard to used e.g. the filtering capabilities of a shell but the convenience of GUI file browsers, but for this I advice you to read xclip -help.

If you use the ZSH and want to save some typing, use The following to set up a alias in your .zshrc:

alias -g CLIP='xclip -d :0 -selection clipboard'

You can then easily execute:

echo "hi" | CLIP

Or, if you want to save you quoting trouble on the shell, just invoke "CLIP" alone, paste your content, press return and Ctrl+D.

I guess you also want to use the other way around. In Vim, you can paste text from the GUI when you add the following to your ~/.vimrc:

com Paste  :call CopyX()
func! CopyX()
    exe ":s/$/\r/g"
    exe ":.!xclip -selection clipboard -d :0 -o"

Executing :Paste will then paste text from the X clipboard to your Vim buffer below the current line.

You can also use the clipboard from GNU screen. The following is based on a blog article. and uses instead xclip. Put the following into your ~/.screenrc:

# read and write screen clipboard to X clipboard.
bind > eval writebuf "exec sh -c 'xclip -d :0 -selection clipboard < /tmp/.screen-exchange'"
bind < eval "exec sh -c 'xclip -selection clipboard -o >/tmp/.screen-exchange'" readbuf

Then you can use Ctl-A + > or < to easily get the contents into or out of the screen clipboard buffer.

Powered by Debian GNU/Linux
© Sebastian Humenda, last modified 02/04/2014