Search code examples
vimsshclipboardxclip

Vim, yanking with xclip, getting rid of escape characters


I am using vim to edit files on several systems through ssh, some of which have or do not have -clipboard option compiled in. Therefore I have been trying to use xclip to copy some parts of the text into the X clipboard, which is then promptly piped back to my machine though ssh, where I can use it.

Now, I know, I can use

:'<,'>w !xclip<CR><CR>

If I want to copy the whole line or lines into clipboard, but I don't want to copy whole lines, I want an arbitary selection

To fix it, I've made a binding, which saves the selection into the @i register and pipes it into the xclip

:vmap <F7> "iy<CR>:call system('echo ' . shellescape(@i) . ' \| xclip -i')<CR>

It seems to work fine, for the selection of a text within one line, but it seems like the escape characters are preserved and when I am copying a several lines like for example

Plugin 'vim-scripts/buftabs'
Plugin 'jlanzarotta/bufexplorer'
Plugin 'kien/ctrlp.vim'
Plugin 'terryma/vim-multiple-cursors'

I get the following in my xclip - the escapes for the newlines are still there

Plugin 'vim-scripts/buftabs'\
Plugin 'jlanzarotta/bufexplorer'\
Plugin 'kien/ctrlp.vim'\
Plugin 'terryma/vim-multiple-cursors'\

I tried using several variations of shellescape and escape, but all of them seem to leave the escaping characters in place. I don't know vimscripting enough to fix it by myself.

I know there are several questions regarding vim and xclip already, but in none of them could I find the answer.

Thanks in advance!


Solution

  • Just by accident, I found the answer to my own question here. Copying to xclip can be done as simply as:

    :vmap <F7> y: call system("xclip -i", getreg("\""))<CR>
    

    And here is a fix, that stops system call from displaying in the statusline

    :vmap <silent> <F7> y :silent call system("xclip -i", getreg("\""))<CR>