Vim - mapping with an optional register prefix

前端 未结 2 373
孤城傲影
孤城傲影 2021-01-20 02:13

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 

        
相关标签:
2条回答
  • 2021-01-20 02:35

    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>
    
    0 讨论(0)
  • 2021-01-20 02:47

    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'
    
    0 讨论(0)
提交回复
热议问题