vim find a line that has this but not that
To find lines which have “This” and “NotThat” it is as simple as:
:g/This\&\(.*NotThat\)\@!
Let’s dive into each piece of this in a little more detail…
The :g is a way to list out all matches in a file. For instance to see all lines listed out which match “This” you could use:
:g/This
Furthermore, if you wanted lines that contained both “This” and “That” then you could use:
:g/This\&\(.*That\)
The reason why the .* is needed is that it looks for the conjoined match (the ampersand) from the previous match. You will notice that the highlighting/match will select the range, i.e.
we use brackets to show the match from [This to That] nothing else matches
By adding the \@! we tell vim to negate that term and viola we are search for “This” and not “That” e.g.
:g/This\&\(.*That\)\@!
Would match the second line, but not the first:
we use brackets to show the match from This to That nothing else matches on this line we only have This and thus it will match
Notice that only the second line will match.
Categories