Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find first non-matching line in VIM

Tags:

vim

search

It happens sometimes that I have to look into various log and trace files on Windows and generally I use for the purpose VIM.

My problem though is that I still can't find any analog of grep -v inside of VIM: find in the buffer a line not matching given regular expression. E.g. log file is filled with lines which somewhere in a middle contain phrase all is ok and I need to find first line which doesn't contain all is ok.

I can write a custom function for that, yet at the moment that seems to be an overkill and likely to be slower than a native solution.

Is there any easy way to do it in VIM?

like image 301
Dummy00001 Avatar asked Jun 23 '10 13:06

Dummy00001


2 Answers

I believe if you simply want to have your cursor end up at the first non-matching line you can use visual as the command in your global command. So:

:v/pattern/visual 

will leave your cursor at the first non-matching line. Or:

:g/pattern/visual 

will leave your cursor at the first matching line.

like image 111
Neg_EV Avatar answered Sep 25 '22 03:09

Neg_EV


you can use negative look-behind operator @<!

e.g. to find all lines not containing "a", use /\v^.+(^.*a.*$)@<!$

(\v just causes some operators like ( and @<! not to must have been backslash escaped)

the simpler method is to delete all lines matching or not matching the pattern (:g/PATTERN/d or :g!/PATTERN/d respectively)

like image 20
mykhal Avatar answered Sep 22 '22 03:09

mykhal