VIM delete all lines contains a pattern

Sometimes you need to tidy up a piece of a text which contains many repetitive lines. For editor like Notepad there is no way you can do it, because it's not composable. Other advanced editors may allow you do that, but for my experience, no one do this better than VIM.

If you think about the problem, you will find it's a programming problem, you actually need to write some code to do it as the pseudo code shows.

occur_lines = search(pattern)
foreach line in occur_lines

Most editors are not programmable, Emacs is programmable, but it use LISP, LISP maybe a great language, but to write LISP code to do such a thing still cumbersome, you can code it but it's not worth it. LISP is great if you need to implement a very complex feature which need hundreds of lines of code.

For such small task, LISP is not efficient.

VIM is also programmable, but it's higher level, modular and easier to use. Here is how to do it in VIM


The g means global, then is the pattern to match, global means match all occurrences, the d means delete.

It's a very compact language, you can type it whenever you want to do something, it's quick, you can accomplish the coding while you are thinking the actions you want to do. LISP can't do that.

Delete all empty lines

The pattern to match empty line: ^$


If you treat lines that contains only whitespaces as empty line


If you are using evil mode in Emacs the pattern will be


Because it uses the Emacs regular expression syntax, \s- represents whitespace.