Re-bind VIM system clipboard register yank/paste keys

473 Views Asked by At

Instead of typing

"*y
"*p

To copy code from one file to another, what kind of VIM shortcut would you guys recommend implementing in my .vimrc which won't interfere with many other motions?

2

There are 2 best solutions below

3
Amadan On

No shortcut. Stick this into your config, then just use the normal y and p:

set clipboard+=unnamedplus

(:help clipboard, :help clipboard-unnamedplus)

(Some prefer unnamed to unnamedplus.)

0
D. Ben Knoble On

I use <C-y> as an operator that performs the yank into "*. I've not bound paste, though I suppose one could do

nnoremap <C-P> "*p

or something similar. Pasting, though, has so many variants that I'm reluctant to "limit" myself. (I use ]op—from tpope/unimpaired—followed by Command-V sometimes, as well as other paste methods.)

For <C-y>, I have the following script in ~/.vim/plugin/pbcopy_op.vim:

if exists('g:loaded_pbcopy_op')
  finish
endif
let g:loaded_pbcopy_op = 1

nnoremap <silent> <C-y> :set operatorfunc=pbcopy#operator<CR>g@
nnoremap <silent> <C-y>y :set operatorfunc=pbcopy#operator<CR>g@_
nnoremap <silent> <C-y><C-y> :set operatorfunc=pbcopy#operator<CR>g@_
vnoremap <silent> <C-y> :<C-u>call pbcopy#operator(visualmode(), 1)<CR>

And the function definition (~/.vim/autoload/pbcopy.vim):

function! pbcopy#operator(type, ...) abort

  let l:visual = a:0

  if l:visual
    normal! gv"*y
  elseif a:type == 'line'
    '[,']yank *
  else " char
    normal! `[v`]"*y
  endif

endfunction

If you're copying from one file to another inside the same vim instance, just use y and p.


Last alternative: I use tmux(1), so I frequently yank things into tmux's buffers.

Then, since I have tpope/tbone installed, I can do :Tput.