created June 21, 2006 · complexity basic · author brudermarkus · version 5.7
Sometimes I need to copy and paste stuff between Vim sessions. I like to use the classic copy-paste keys for this:
- Hit Ctrl+C to copy the current line or current visual selection.
- Hit Ctrl+V to paste the contents of the previous copy action.
In recent versions of Vim, you can use the system clipboard. This should let you copy and paste between Vim and applications other than Vim.
vmap <C-c> "*y " Yank current selection into system clipboard nmap <C-c> "*Y " Yank current line into system clipboard (if nothing is selected) nmap <C-v> "*p " Paste from system clipboard
These mappings work in "normal" and "visual" mode.
You do not have to map these commands to keys. You could type "*y and "*p directly, and avoid overwriting existing key mappings.
If the above method does not work, here is a custom method for copying data between separate vim sessions, which uses a file to store and retrieve the data:
"custom copy'n'paste vmap <C-c> :w! ~/.vbuf<CR> "copy the current visual selection to ~/.vbuf nmap <C-c> :.w! ~/.vbuf<CR> "copy the current line to the buffer file if no visual selection nmap <C-v> :r ~/.vbuf<CR> "paste the contents of the buffer file
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.