Make vim keep mark when I delete the line the mark is on

前端 未结 2 1613

How can I get vim to preserve marks when I delete the line the mark is on (i.e., automatically move the mark to the line immediately above or below the marked line)

2条回答
  •  庸人自扰
    2021-01-22 22:54

    One addition to @Kurt Hutchinson answer: there is a possibility to define an operator function. In this case you don’t need to define a mapping for every deletion command, but instead redefine d:

    let s:typetochar={
                \ 'char': 'v',
                \ 'line': 'V',
                \'block': "\",
            \}
    function! DeleteOperator(type)
        execute 'lockmarks normal! `["'.v:register.'d'.s:typetochar[a:type].'`]'
    endfunction
    function! s:Delete()
        set opfunc=DeleteOperator
        return 'g@'
    endfunction
    nnoremap  d  Delete()
    nnoremap  dd Delete().'g@'
    vnoremap d :lockmarks normal! gvd
    

    Note the dd mapping: as d is actually using g@, there should be dg@ (expanded to g@g@) where you type dd. So in order to use cleaner dd you have to create a mapping.

提交回复
热议问题