created December 6, 2004 · complexity basic · author Shin Seung Woo · version 6.0
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
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