Skip to content

Instantly share code, notes, and snippets.

@y0mbo
Forked from shreeshga/delete_lines.md
Created January 30, 2018 16:55
Show Gist options
  • Save y0mbo/18a0355c63db19b2986c501bafb6aca8 to your computer and use it in GitHub Desktop.
Save y0mbo/18a0355c63db19b2986c501bafb6aca8 to your computer and use it in GitHub Desktop.
Delete all lines matching pattern in vim

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

For example, to delete all lines containing "profile" (the first command is optional; it shows the lines that the second command will delete):

:g/profile
:g/profile/d

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

:g/^\s*$/d

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 g! is equivalent to v, so you could also do the above with:

:v/^\s*"/d

The next example shows use of | ("or") to delete all lines ''except'' those that contain "error" or "warn" or "fail" ({{help|pattern}}):

:v/error\|warn\|fail/d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment