Is it possible to interactively delete matching search pattern in Vim?

前端 未结 4 1345
暗喜
暗喜 2021-01-30 04:01

There is a phrase that I want to look for in Vim. When found, I want to delete that occurrence of the phrase. What is the easiest way to cycle through all the occurrences (via <

4条回答
  •  庸人自扰
    2021-01-30 04:33

    There are 3 ways I can think of:

    The way that is easiest to explain is

    :%s/phrase to delete//gc
    

    but you can also (personally I use this second one more often) do a regular search for the phrase to delete

    /phrase to delete
    

    Vim will take you to the beginning of the next occurrence of the phrase.

    Go into insert mode (hit i) and use the Delete key to remove the phrase.

    Hit escape when you have deleted all of the phrase.

    Now that you have done this one time, you can hit n to go to the next occurrence of the phrase and then hit the dot/period "." key to perform the delete action you just performed

    Continue hitting n and dot until you are done.

    Lastly you can do a search for the phrase to delete (like in second method) but this time, instead of going into insert mode, you

    Count the number of characters you want to delete

    Type that number in (with number keys)

    Hit the x key - characters should get deleted

    Continue through with n and dot like in the second method.

    PS - And if you didn't know already you can do a capital n to move backwards through the search matches.

提交回复
热议问题