Fandom

Vim Tips Wiki

Unconditional linewise or characterwise paste

Redirected from VimTip1199

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

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 alsoEdit

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

Related PluginsEdit

CommentsEdit

Also on Fandom

Random Wiki