Vim - mapping with an optional register prefix

前端 未结 2 371
孤城傲影
孤城傲影 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 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)
      "  appends, but  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  P :call PutAt(1)
    nnoremap  p :call PutAt(line('$')+1)
    

提交回复
热议问题