Vim: replace string pattern without string

I am trying to achieve a simple replacement for vim, but I can not get it right. I need to delete all lines matching the pattern on the entire file. A pattern is β€œsomething,” which means β€œsomething,” followed by something to the end of the line. I tried :%s/pattern*\n//g and :%s/pattern*$//g with no success. Any ideas?

Hooray!

+7
source share
2 answers

Use :g instead of :substitute .

 :g/pattern/d 

will delete all lines matching the pattern.

As for the pattern, yours will match patter , pattern , patternn and so on. Use a wildcard . to match any characters. So your regex should be pattern.*$ ---, but if you want to completely delete the lines :g/pattern/d does the trick in order.

+6
source

Depending on the version of vim you are using, you may have problems with what I'm going to suggest:

 :%s/pattern.*^V^M//g 

For ^V , literally press ctrl-v and then press ENTER (^ M). ^V allows you to enter an alphabetic character without interpreting the character.

0
source

All Articles