3

So I've discovered that a common task for me in Vim is to PUT either to the start of the line or the end of the line. So my mapping could be:

nmap <Leader>p $p
nmap <Leader>P 0P

However, what I'd really like to do is optionally include a register before putting.

So for example "a,P would put from register a to the beginning of the line.

Is there a way do this with a mapping?

Richard
  • 2,994
  • 1
  • 19
  • 31

2 Answers2

6

You can do this using <expr> mapping in one line:

nnoremap <expr> \p '$"'.v:register.v:count1.'p'
nnoremap <expr> \P '0"'.v:register.v:count1.'P'
ZyX
  • 52,536
  • 7
  • 114
  • 135
2

This is perfectly possible. I first I though this solution was possible: https://stackoverflow.com/a/290723/15934, but <expr> won't let us move the cursor as we wish, and normal can't be used.

Still, we can do this:

function! s:PutAt(where)
  " <setline($+1> appends, but <setline(0> does not insert, hence the hack
  " with getline to build a list of what should be at the start of the buffer.
  let line = a:where ==1 
        \ ? [getreg(), getline(1)]
        \ : getreg()
  call setline(a:where, line)
endfunction

nnoremap <silent> <leader>P :call <sid>PutAt(1)<cr>
nnoremap <silent> <leader>p :call <sid>PutAt(line('$')+1)<cr>
Community
  • 1
  • 1
Luc Hermitte
  • 31,979
  • 7
  • 69
  • 83
  • He was talking about adding at the beginning/end of the line, not the file. And, by the way, `` mappings do allow you to move a cursor, you should just add cursor moving commands to the result of the expression. – ZyX Mar 31 '12 at 16:33