vimtricks.wiki Concise Vim tricks, one at a time.

How do I delete the line immediately following every line that matches a pattern?

Answer

:g/pattern/+1d

Explanation

Using :g/pattern/+1d you can delete the line that comes right after each line matching a pattern, in one pass. The trick exploits the fact that :g accepts an arbitrary Ex address for its sub-command: +1 is a relative offset meaning "current line + 1", and d deletes it.

How it works

  • :g/pattern/ — mark every line that matches the pattern
  • +1d — for each marked line, delete the line at .+1 (one line below the match)

Vim processes matches from top to bottom, adjusting line numbers after each deletion, so the offsets stay accurate throughout.

Example

Given a log file where each ERROR: heading is followed by a redundant path line you want to strip:

ERROR: connection refused
  at /usr/lib/app.js:42
ERROR: timeout
  at /usr/lib/app.js:17

Running:

:g/^ERROR:/+1d

Produces:

ERROR: connection refused
ERROR: timeout

Tips

  • Use +2d or -1d to target two lines below or one line above each match
  • Chain with a range: :g/pattern/.,+2d deletes the matching line AND the two that follow
  • Use :v/pattern/+1d (the inverse global) to delete the line after each line that does NOT match
  • Be aware that if two consecutive lines both match, the first deletion shifts subsequent marks — test on a copy first for critical data

Next

How do I configure Vim's command-line tab completion to show all matches and complete to the longest common prefix?