sponsor Vim development Vim logo Vim Book Ad

basic Tip #829: copy & paste between vim session

 tip karma   Rating 13/6, Viewed by 4561 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   December 6, 2004 7:33      complexity:   basic
author:   Shin Seung Woo      as of Vim:   6.0

I use putty + vim + screen on web application developing.
because, daily working directory is more than 10. I couldn't find easy way
to develop in only one vim session.

so, copy & paste between session, usally 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

but, I think there are more simple way to this in our vim world. always there were.

what's the best way to copy & paste between vim sessons?

 rate this tip  Life Changing Helpful Unfulfilling 

<< pad trailing blanks onto end of lines to ease visual blocks | Using netbeans 4 and vim for java >>

Additional Notes

Anonymous, December 7, 2004 2:04
You can use the selection and drop registers to copy between sessions.

e.g. "*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
Anonymous, December 8, 2004 23:32
Note that he is logging in remotely!! Selecting something on his local maching and typing "*p will not work...
[email protected], December 13, 2004 6:59
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
[email protected], December 15, 2004 10:41
> I'm using vim on local computer and * register somehow des not work for me.

Not sure if this is the case, but on my system vim & gvim are different executables.
And * registrer works only in X11 enabled builds.

Solution is to rename vim to vimold, and symlink (or copy) gvim to vim.
Thus, vim will run X11 enabled binary (with access to X11 clipboard through * register),
but still in text-olny interface.

You can do a fast check by running 'gvim -v'.
[email protected], December 18, 2004 2:25
I don't have gvim built at all, so that's probably the reason. Maybe I missed this in the documentation...
Anonymous, December 23, 2004 16:24
A side note about pasting from the gui clipboard into a remote vim, try using
:insert
then press return and paste the text from the gui and then press escape.
This should get the text pasted without auto-indenting it.
[email protected], January 4, 2005 16:57
vlmarek wrote --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>

.....
--
This is a good script and works well between buffers. Any way of getting it to work between different windows. Using gvim on sun solaris. Any suggestions?
[email protected], January 27, 2005 9:53
If you use
set clipboard=unnamed
in your .vimrc, it's like automatically using "* (the clipboard register) for all yank and paste operations.

Of course this needs a GUI enabled vim (and on Unix a connection to your X11 server, but this is possible through ssh with X11Forwarding, too)
[email protected], August 22, 2005 12:35
Maybe I miss the point of this discussion, but when I need to paste between sessions I select visual block, then punch
:{vim inserts'<,'> here}w! ~/cl

and to paste (in different session, or even same session, does not matter)
:.r ~/cl

I thought about mapping this to some keystroke sequence, but I do not need it that often.

[email protected], November 13, 2006 1:45
you can use shift + mouse_selection to get putty to behave as the usual copy/paste.

from the putty user manual:

"PuTTY allows the server to send control codes that let it take over the mouse and use it for purposes other than copy and paste. Applications which use this feature include the text-mode web browser links, the Usenet newsreader trn version 4, and the file manager mc (Midnight Commander).

If you find this feature inconvenient, you can disable it using the ‘Disable xterm-style mouse reporting’ control. With this box ticked, the mouse will always do copy and paste in the normal way.

---> Note that even if the application takes over the mouse, you can still manage PuTTY's copy and paste by holding down the Shift key while you select and paste, unless you have deliberately turned this feature off"
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo