Vim search for a pattern and if occurs delete to end of line

后端 未结 4 1786
鱼传尺愫
鱼传尺愫 2021-01-31 08:25

I am trying to search a text file for a certain pattern. If this pattern occurs then it means that the rest of the line is not needed and therefore can be deleted.

I ha

相关标签:
4条回答
  • 2021-01-31 08:25
    %s/\\(replay-pattern-later\\) pattern2/\1/gc
    

    This will replay pattern1 but eliminate pattern2. Using "/gc" to apply globally (keep going) and prompt for each occurence.

    0 讨论(0)
  • 2021-01-31 08:39

    Alternatively, the following also works

    :g/{pattern}/normal nd$
    

    For what you want, I would go with mirod's suggestion. What I posted is a bit more flexible and might come in handy in similar situations.

    Explanation:

    On each line, where pattern matches, execute the following normal mode commands 'nd$'. With the cursor at the start of the line, 'n' jumps to the pattern, and 'd$' deletes to the end of the line.

    0 讨论(0)
  • 2021-01-31 08:48

    To replace a pattern and whatever else use the regular expression wild card .*

    :1,$s/pattern.*//g
    
    0 讨论(0)
  • 2021-01-31 08:49

    would :%s/{pattern}.*// work?

    0 讨论(0)
提交回复
热议问题