Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Find any line NOT ending in "WORD"

Tags:

vim

In vim, how can I search for any line that does not end with (in this instance) the word "DROP"?

like image 985
apprentice454 Avatar asked Apr 26 '12 06:04

apprentice454


People also ask

How do I jump to a specific line in Vim?

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.

How do I delete a line not matching in Vim?

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.

What does F do in Vim?

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.

How do I go back to the last line in Vim?

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).


1 Answers

/\(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:

  • http://www.regular-expressions.info/lookaround.html

You can also type this in VIM to get help on the command:

:help \@<!

Similar assertions:

\@=
\@<=
\@!
like image 105
Merlyn Morgan-Graham Avatar answered Oct 18 '22 08:10

Merlyn Morgan-Graham