Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine and negate these two patterns together?

Tags:

vim

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'

like image 214
marlon Avatar asked Aug 19 '20 21:08

marlon


1 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
like image 143
filbranden Avatar answered Sep 17 '22 16:09

filbranden