tags:

views:

89

answers:

4

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?

+1  A: 

I just managed a somewhat klutzy procedure using the "g" command:

:%g!/search/p

This says to print out the non-matching lines... not sure if that worked, but it did end up with the cursor positioned on the first non-matching line.

(substitute some other string for "search", of course)

Carl Smotricz
And `:v` is a synonym for `:g!`, of course.
Jefromi
@Jefromi: Thanks for that, I'd forgotten about `:v`.
Carl Smotricz
+2  A: 

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)

mykhal
`/^.\+\(^.*all is ok.*$\)\@<!$` works as desired - thanks a bunch!
Dummy00001
`:g!/PATTERN/p` produces too much output. for a few mln line file that isn't an option. but `:g!/PATTERN/d` looks very useful e.g. for working with buffer read from stdin, not saved to a file yet.
Dummy00001
Uhm... obviously the `:g!/PATTERN/d` for my case (ignore the uninteresting lines macthing PATTER) should be written as `:g/PATTERN/d` - without ! - to remove the uninteresting lines.
Dummy00001
+2  A: 

I'm often in your case, so to "clean" the logs files I use :

:g/all is ok/d

Your grep -v can be achieved with

:v/error/d

Which will remove all lines which does not contain error.

Drasill
+1  A: 

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.

Neg_EV
the visual command can also be shortened to vi... so you can do :v/pattern/vi
Neg_EV