Take the 2-minute tour ×

I'm trying to achieve a simple substitution on vim but can't get it right. I need to remove, on an entire file, all the lines that match a pattern. The pattern is "something*", meaning "something" followed by anything until the end of the line. I tried :%s/pattern*\n//g and :%s/pattern*$//gwithout success. Any ideas?


share|improve this question
FYI: The * applies to the previous character in your pattern, so in your case it will match zero or more n's. –  Brian Rasmussen May 5 '11 at 8:51
Right, I always get confused with shell expansion characters :/ –  brafales Oct 18 '11 at 7:11
add comment

2 Answers

up vote 5 down vote accepted

Use :g instead of :substitute.


would remove all the lines that match with pattern.

As for the pattern, yours will match patter, pattern, patternn and so on. Use the wildcard . to match any characters. So your regexp should be pattern.*$ --- but if you wish to remove the lines entirely, the :g/pattern/d does the trick fine.

share|improve this answer
Thanks, just what I was looking for, gotta get used to read more manuals! ;) –  brafales May 5 '11 at 9:02
@brafales, yeah. :g is the other half of the ex magic. :) A true hidden powerhouse that can do interesting tricks. –  progo May 5 '11 at 9:06
One addition: g/pattern/d _ (note the underscore) will act just the same as s/.*pattern.*\n\=//g, while your version will spoil values in unnamed, numbered (1-9) and, probably, some other registers. –  ZyX May 5 '11 at 17:37
add comment

Depending on the version of vim you're using, you may have trouble with what I'm going to propose:


For ^V, literally hit ctrl-v, followed by the ENTER (^M) key. ^V lets you enter a literal character without it interpreting the character.

share|improve this answer
Dot means «any symbol except newline», so no need to use such collection. –  ZyX May 5 '11 at 17:39
Good point. Changed it back to the way I had it before. –  Brian Vandenberg May 5 '11 at 18:38
add comment

Your Answer


