Vim Tips Wiki
Register
Advertisement
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Tip 829 Printable Monobook Previous Next

created 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.


You can use :set paste and :set nopaste to tell vim to not autoindent or otherwise alter the format of your pasted text.


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

---

It is much easier to do this. It has limitations, like hogging the x register and only yanks in visual mode, but it'll work.

:vmap <silent> ,y "xy<CR>:wviminfo! ~/.viminfo
:nmap <silent> ,p :rviminfo! ~/.viminfo<CR>"xp

---

If you're super lazy (like me) and you're working in bash or some such nice terminal...just select and copy/paste using the terminal commands (select with mouse, edit-copy, edit-paste). I know, its a cop-out for command line editing, but it's freaking easy and it works.

Advertisement