Vim Tips Wiki
Advertisement
Tip 1199 Printable Monobook Previous Next

created 2006 · complexity basic · author cory · version 6.0


If you're like me, you occasionally do a linewise yank, and then want to insert that yanked text in the middle of some other line, (or vice versa). This function and mapping will allow you to do a linewise or characterwise paste no matter how you yanked the text.

function! Paste(regname, pasteType, pastecmd)
  let reg_type = getregtype(a:regname)
  call setreg(a:regname, getreg(a:regname), a:pasteType)
  exe 'normal "'.a:regname . a:pastecmd
  call setreg(a:regname, getreg(a:regname), reg_type)
endfunction
nmap <Leader>lP :call Paste(v:register, "l", "P")<CR>
nmap <Leader>lp :call Paste(v:register, "l", "p")<CR>
nmap <Leader>cP :call Paste(v:register, "v", "P")<CR>
nmap <Leader>cp :call Paste(v:register, "v", "p")<CR>

The above will keep newlines inside the register. To additionally join the pasted lines to a stream of characters without any newlines, use this:

function! PasteJointCharacterwise(regname, pastecmd)
  let reg_type = getregtype(a:regname)
  call setreg(a:regname, '', "ac")
  exe 'normal "'.a:regname . a:pastecmd
  call setreg(a:regname, '', "a".reg_type)
  exe 'normal `[v`]J'
endfunction
nmap <Leader>p :call PasteJointCharacterwise(v:register, "p")<CR>
nmap <Leader>P :call PasteJointCharacterwise(v:register, "P")<CR>

See also[]

  • VimTip356 Quick yank and paste (will be "copy/paste mappings" tip)

Related Plugins[]

Comments[]

Advertisement