Vim Tips Wiki
Register
Advertisement
Tip 1268 Printable Monobook Previous Next

created June 21, 2006 · complexity basic · author brudermarkus · version 5.7


Sometimes I need to copy and paste stuff between Vim sessions. There are some proposals on how to do this with the clipboard, but here is an alternative (should work for any OS):

  • Hit Ctrl+C to copy the current line or current visual selection (it will be saved to the file ~/.vbuf).
  • Hit Ctrl+V to paste the contents of the previous copy action.

These mappings work in "normal" and "visual" mode.

"custom copy'n'paste
"copy the current visual selection to ~/.vbuf
vmap <C-c> :w! ~/.vbuf<CR>
"copy the current line to the buffer file if no visual selection
nmap <C-c> :.w! ~/.vbuf<CR>
"paste the contents of the buffer file
nmap <C-v> :r ~/.vbuf<CR>

Comments

Registers are stored in the viminfo file. Isn't this a more functional approach? See :help viminfo, :help 21.3.


Extremely useful - I use a variation of this every day: VimTip66


This is fine when you're on a system that maps copy and paste to ctrl-c or ctrl-v.

By default Vim doesn't do that. There's overrides to allow it to do so on Mac and Windows which makes Vim think I want to quit or then messes up the ability to do column selections on Linux.

I prefer sticking with the "+Y and "*Y methods to yank into standard registers. Those work consistently on Vim on Linux, Mac and Windows.


Advertisement