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

前端 未结 2 1604

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:43

    Maybe the easiest way is to use capital letter marks when you don't want them deleted. If the line the mark is on is deleted, it gets moved to the next line.

    Another option is the lockmarks command. lockmarks takes a command to run and locks most marks at their current line number until the command completes. If you wanted to do that often, you'd probably need to add some mappings that would lockmarks for you, for example this turns dd into mark-preserving:

    nnoremap dd :lockmarks normal! dd
    

    or

    nnoremap dd :lockmarks delete
    

    The problem is that you lose the ability to specify a count for dd. It's possible to get around that by using v:count, but then we'll need execute to get that interpolated:

    nnoremap dd :execute 'lockmarks normal! ' . v:count . 'dd'
    

    And then you'd have to define a mapping like this for every deletion operation you use regularly.

    So, probably using capital letter marks will be easier. You'll just have to remember to use them.

提交回复
热议问题