In vim, how can I search for any line that does not end with (in this instance) the word "DROP"?
If we are in the vim editor, then simply do this, “Press the ENTER key, write the Line number, and press Shift+ g”: Again the output is the same.
The ex command g is very useful for acting on lines that match a pattern. You can use it with the d command, to delete all lines that contain a particular pattern, or all lines that do not contain a pattern.
The f command is very similar to the t command, but it places your cursor on top of the character. The F command works how you might expect, moving your cursor backwards and on top of the next occurrence of the selected character.
Example. To jump back to line # 300 or previous position press CTRL-O (press and hold Ctrl key and press letter O). To jump back forwards press CTRL-I (press and hold Ctrl key and press letter I).
/\(DROP\)\@<!$
This uses a zero-width negative look-behind assertion. It finds just the line ending, and only finds line endings that don't have DROP
immediately preceding them.
If you want to find the whole line, you can use:
/^.*\(DROP\)\@<!$
Note that you have to surround DROP
with \( .. \)
because look-ahead and look-behind assertions will only match a single "atom". So you use the parens to group your word into a single atom.
If you tried /DROP\@<!$
, then you'd get search results like the bold part here:
abcdef
test test DRO
12345DROP
12345DRO
12345
There's a tutorial for these assertions on this page, though it doesn't use VIM regex syntax:
You can also type this in VIM to get help on the command:
:help \@<!
Similar assertions:
\@=
\@<=
\@!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With