开发者

vim: alias yank and copy to xclip

I've got a cntrl c and cntrl v mapped to xclip, however its a hassle to have to remember to use instead of regular y and p. Is there a way to alias the two or send contents of y and p to xclip, so I can just use y and p for all copy and开发者_如何转开发 pasting?

vmap <C-c> y:call system("xclip -i -selection clipboard", getreg("\""))<CR>:call system("xclip -i", getreg("\""))<CR>
nmap <C-v> :call setreg("\"",system("xclip -o -selection clipboard"))<CR>p")")")"))


Are you trying to use the X clipboard for all copy and pastes? If so, a good alternative to xclip is to make sure you're using a vim with X support (it's really easy to compile Vim if your version doesn't have it) and then add the following to your vimrc:

set clipboard=unnamed

All yanks and deletes will then automatically go to the * register (which is the X selection register).

Instead of setting clipboard=unnamed, you can also use the X selection register for a single operation by using (e.g.)

"*yw
"*yy
"*ya(

or whatever.

Obviously, this doesn't answer your question as to how to use xclip, but hopefully it offers an alternative approach.


I don't know if this is an ideal solution, but it works.

set clipboard=unnamedplus

function! ClipboardYank()
  call system('xclip -i -selection clipboard', @@)
endfunction

let vlcb = 0
let vlce = 0
function! ClipboardPaste(mode)
  if (a:mode == "v")
    call cursor(g:vlcb[0], g:vlcb[1]) | execute "normal! v" | call cursor(g:vlce[0], g:vlce[1])  
  endif
  let @@ = system('xclip -o -selection clipboard')
endfunction

" replace currently selected text with default register without yanking it
vnoremap <silent>p "_dP

vnoremap <silent>y y:call ClipboardYank()<CR>
vnoremap <silent>d d:call ClipboardYank()<CR>
nnoremap <silent>dd dd:call ClipboardYank()<CR>

nnoremap <silent>p :call ClipboardPaste("n")<CR>p
vnoremap p :<C-U>let vlcb = getpos("'<")[1:2] \| let vlce = getpos("'>")[1:2] \| call ClipboardPaste("v")<CR>p


You can also set clipboard=unnamedplus to use the “+” register by default. Using it, I did yy in my .vimrc and then pasted that line here by ctrl-V ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜