2010-07-13

Vim Delete Lines With Regex

To use vim to delete all lines that have a specific string: :g/s1/d. To delete all lines that don't have a specific string, add an exclamation mark before the string: :g!/s1/d. If there are alternative strings to match (logical OR), insert an escaped vertical bar, e.g. :g/s1\|s2/d.

2 comments:

  1. A ! can make so much difference. Let us hope vim has a good undo command...

    So can you specify lines? I have some dim memory of having to go 1,G or something like that.

    ReplyDelete
  2. Undo works for 'ex' (or colon-based) commands, too.

    You can constrain a command to a range of lines, e.g. line 4 to the last line, like this:
    :4,$g/s1/d

    ReplyDelete