FANDOM


Tip 213 Printable Monobook Previous Next

created February 10, 2002 · complexity basic · author tarjei · version 5.7


The ex command, 'g' is very useful for acting on lines that match a pattern. You can use it with the ex command, 'd' to delete all lines that contain/do not contain a particular pattern.

For a trivial example, delete all lines containing "profile":

:g/profile/d

Obviously, more complex patterns can be used, such as deleting all lines that are empty or contain only whitespace:

:g/^\s*$/d

Finally, to delete all lines that do NOT contain a pattern, use g!, like this command to delete all lines that are not comment lines in a Vim script:

:g!/^\s*"/d

Note that the 'g!' command is equivalent to the 'v' command, so you could also do the above with:

:v/^\s*"/d

See also

Comments

Community content is available under CC-BY-SA unless otherwise noted.