vim: clipboard support without x

June 30, 2014

On my Linux computers I have used xclip for having clipboard support for some years but never got it to run in my servers because they do not have X server and you need one for the clipboard, always got a:

xclip: Error: Can't open display: (null)

So I had to do a workaround and the easiest thing to do was using a temporary file:

if system("echo $DISPLAY") =~ ""
  vmap <C-c> y: call system("> /tmp/theClipboardWithoutX", getreg("\""))<CR>
  map <C-v> :call setreg("\"", system("< /tmp/theClipboardWithoutX"))<CR>p
else
  vmap <C-c> y: call system("xclip -i -selection clipboard", getreg("\""))<CR>
  map <C-v> :call setreg("\"",system("xclip -o -selection clipboard"))<CR>p
endif

Basically: if there's no $DISPLAY set then send the copied selection to the file /tmp/theClipboardWithoutX with the keybinds Ctrl+C and with Ctrl+V you get the data inside that file. If there's $DISPLAY then use xclip.

Surprisely it works very well.

Check my full vimrc for copy and paste stuff (has configs for Mac as well) and if you want, the full vimfiles repo may come handy as well.

More posts

See the archive for more posts