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
.
A ! can make so much difference. Let us hope vim has a good undo command...
ReplyDeleteSo can you specify lines? I have some dim memory of having to go 1,G or something like that.
Undo works for 'ex' (or colon-based) commands, too.
ReplyDeleteYou can constrain a command to a range of lines, e.g. line 4 to the last line, like this:
:4,$g/s1/d