How to combine and negate these two patterns together?

Viewed 73

In VIM, I want to delete any lines that are not 2 or 3 characters.

:g/^..$/d
:g/^...$/d

Those delete 2 or 3 character lines. How to combine the two into one and negate it, namely 'don't delete 2 or 3 character lines'

3 Answers

You can use :v to execute a command on lines that do not match a pattern.

This requires that you use a single pattern though... Which in your case you can easily do by using the \= modifier to optionally match the last item.

So to delete all lines with either 2 or 3 characters, you can use:

:g/^...\=$/d

And to delete all lines except those with either 2 or 3 characters:

:v/^...\=$/d

How about "delete all lines with less than two or more than three characters"?

:g/^.\{,1}$\|^.\{4,}/d
Related