yyp pastes the current line underneath itself and moves the cursor to the start of the new line.

If I start with my cursor on X:

Ysiml yuipXedicious

after yyp I get

Ysiml yuipXedicious
Ysiml yuipXedicious

But now my cursor is on Y (of the second line)

How can I paste so that my cursor will be on the X of the second line (same column it was on when I pressed p)?


The function name is terrible, but this works:

function! Pcol(...) abort
  let a:above = get(a:, 1, 0)
  let l:col = virtcol('.')
  execute 'normal!' a:above ? 'P' : 'p'
  call cursor('.', l:col)

If Pcol() is called with 0 or no argument, it will paste below the cursor, like p. If called with 1, it will paste above the cursor, like P. If the current cursor position lies beyond the length of the pasted line, it simply moves to the end of the pasted line.

Map the function to whatever you like. You could even just remap p and P if you always want this behavior:

nnoremap <silent> p :call Pcol(0)<CR>
nnoremap <silent> P :call Pcol(1)<CR>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.