Wikia

Vim Tips Wiki

Unconditional linewise or characterwise paste

Talk0
1,613pages on
this wiki
Revision as of 08:56, January 22, 2011 by JohnBot (Talk | contribs)

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>

Content from tip 1306 (now removed; content to be merged here)

 TO DO 

  • Might keep both these approaches, with clear guidance about why each is useful.
  • Or, might merge them.

When you've yanked a text linewise, you can paste it in the middle of another line characterwise and joined. For example: yank 3 lines with 3Y, move cursor into some other line, do \P.

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

Comments

Around Wikia's network

Random Wiki