Skip to main content

How to delete all lines containing/matching a pattern in Vim

Summary

Learn how to use Vim's powerful :global command to delete lines matching specific patterns, covering multiple patterns, ranges, case-(in)sensitivity, and advanced techniques.

Introduction #

Vim is a powerful text editor that offers advanced tools for manipulating text efficiently. One such feature is the ability to delete all lines matching specific patterns using simple commands. This is super usefule, e.g. to clean up log files, remove irrelevant (empty) lines, or streamline a file for analysis.

In this article, I will show you how to use the global command (:global/pattern/command) to execute an Ex command on all lines that match a certain pattern, including handling multiple patterns, specifying ranges, ignoring cases, and more.

Delete all lines by pattern matching #

To delete all lines containing a certain pattern in Vim, follow these steps:

  1. Open the file in Vim, e.g. vim filename
  2. Enter command mode by pressing Esc (if you are not already in it).
  3. Use the following command to delete lines containing the desired pattern:
:g/pattern/d

Replace pattern with the specific text or regex pattern you want to match. The :g command applies the operation globally to all matching lines. The /d deletes the matching lines.

Delete Lines Containing a Certain Word #

To delete all lines containing the word “error”:

:g/error/d

Delete Lines Containing Multiple Words #

To delete all lines containing both “error” and “fatal”, where “error” appears before “fatal” on the same line:

:g/error.*fatal/d

Delete Lines Matching Any Word in a List #

To delete lines containing either “error”, “warning”, or “critical”:

:g/\v(error|warning|critical)/d

The \v flag (pattern modifier) enables “very magic” mode

Delete Lines Starting with a Specific Pattern #

To delete all lines that start with “DEBUG”:

:g/^DEBUG/d

The caret (^) anchors the pattern to the start of the line.

Delete Lines Containing a Pattern in a Specific Range #

To delete lines containing “TODO” between lines 10 and 50:

:10,50g/TODO/d

This applies the delete command only within the specified range of lines.

Delete Lines Containing a Pattern but Exclude Certain Cases #

To delete all lines containing “error” but not those containing “ignore”:

:g/error/if !match(getline('.'), 'ignore') | d | endif

Uses conditional logic to filter out lines containing “ignore”.

Delete Lines Matching a Case-Insensitive Pattern #

To delete all lines containing warning, ignoring case:

:g/\cwarning/d

The \c flag (pattern modifier) makes the search case-insensitive. The \C flag on the other hand, would enforce case-sensitive matching, overriding any case-insensitivity set by options like :set ignorecase.

Delete Lines with Empty or Whitespace-Only Content #

To delete all lines that are empty or contain only spaces/tabs:

:g/^\s*$/d

The ^\s*$ matches lines with only whitespace or nothing at all.


Tips #

Dry Run #

If you want to test the pattern first without deleting lines, use:

:g/pattern

Leaving out the /d will only list matching lines.

Invert Pattern Matching #

To invert the pattern matching and to delete all lines which do not containing the pattern, use:

:g!/pattern/d

Delete lines matching pattern in multiple files at once #

You can also apply the same techniques to multiple files, which are opened in Vim buffer. For example, use the bufdo command, to work on all buffers:

:bufdo %d/pattern/g

To learn more, visit the article about editing multiple files in Vim at once.


Further readings #

Sources and recommended, further resources on the topic:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

License

License: How to delete all lines containing/matching a pattern in Vim by Jonas Jared Jacek is licensed under CC BY-SA 4.0.

This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:

<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/instructions/delete-lines-by-pattern-in-vim">How to delete all lines containing/matching a pattern in Vim</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>

For more information see the DITig legal page.


“Content precedes design. Design in the absence of content is not design, it's decoration.”

 Jeffery Zeldman, American web designer, author, and advocate for web standardsTwitter, - IT quotes