Vim Tips Wiki
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.


Thanks for this tip. I made a change to your script though: replaced Ctrl-c and Ctrl-v with Shift-y and Shift-p. I hope this way I don't overwrite any default vim commands, and it's also "logically" linked to the y and p commands.

Nice try, but shift-y is "yank entire line" and shift-p is "put BEFORE cursor" by default. Vim uses almost the entire keyboard..."safe" keys are pretty much the F2-F12 keys, ALT-key combinations, some CTRL-key combos, and <Leader> mappings. That being said, if there is a command you never use, it should be perfectly fine to overwrite it. See Mapping_keys_in_Vim_-_Tutorial_(Part_2)#Finding_unused_keys.

I'm using this in linux, with multiple instances of vim opened in different consoles, and it's great.


Advertisement