History Report a problem
Article Edit this page Discussion

Copy and paste between Vim instances

From Vim Tips Wiki

Jump to: navigation, search

Tip 829 Previous TipNext Tip

Created: December 6, 2004 Complexity: basic Author: Shin Seung Woo Minimum version: 6.0 Karma: 13/6 Imported from: Tip#829


I use putty + vim + screen on web application developing. I use more than ten working directories, so I couldn't find an easy way to develop in only one vim session.

So, copy & paste between session, usually used Window's clipboard. but, to copy longer than one screen, It's really hard. yes, there are auto-indenting problem too.

And sometimes, I just want copy function definition or long variable names. between vim buffers I simply use visual selection. or cw/ciw. in this case, yanking by line is worse than clipboard.

Last night I made this script saving current register to file supporting visual selection too.

  • ,y saves current unnamed buffer to ~/reg.txt file.
  • ,p / ,P read from ~/reg.txt and paste using p/P.

In visual mode, ,y is yank and save.

vmap <silent> ,y y:new<cr>:call setline(1,getregtype())<cr>o<esc>P:wq! ~/reg.txt<cr>
nmap <silent> ,y :new<cr>:call setline(1,getregtype())<cr>o<esc>P:wq! ~/reg.txt<cr>
map <silent> ,p :sview ~/reg.txt<cr>"zdddG:q!<cr>:call setreg('"', @", @z)<cr>p
map <silent> ,P :sview ~/reg.txt<cr>"zdddG:q!<cr>:call setreg('"', @", @z)<cr>P

[edit] Comments

You can use the selection and drop registers to copy between sessions.

For example, "*yy will copy the current line to the clipboard, then "*p will put whatever is on the clipboard in the document.

You can use any normal motion command or visual selection when yanking. See :help registers.


I'm using Vim on local computer and * register somehow des not work for me. Neither was this mapping, probably because of my vim settings. After some reworking I got something what works for me.

Moreover, it should have the filename defined only on one place, it should not modify any registers beyond ", and it should delete the temporary buffer created.

let g:session_yank_file="~/.vim_yank"
map <silent> <Leader>y :call Session_yank()<cr>
vmap <silent> <Leader>y y:call Session_yank()<cr>
vmap <silent> <Leader>Y Y:call Session_yank()<cr>
nmap <silent> <Leader>p :call Session_paste("p")<cr>
nmap <silent> <Leader>P :call Session_paste("P")<cr>

function Session_yank()
  new
  call setline(1,getregtype())
  put
  silent exec 'wq! ' . g:session_yank_file
  exec 'bdelete ' . g:session_yank_file
endfunction

function Session_paste(command)
  silent exec 'sview ' . g:session_yank_file
  let l:opt=getline(1)
  silent 2,$yank
  if (l:opt == 'v')
    call setreg('"', strpart(@",0,strlen(@")-1), l:opt) " strip trailing endline ?
  else
    call setreg('"', @", l:opt)
  endif
  exec 'bdelete ' . g:session_yank_file
  exec 'normal ' . a:command
endfunction

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pushing Daisies
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Astronomy Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for featured Wikia!

Send this article to a friend
"Copy and paste between Vim instances"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation