tags:

views:

822

answers:

3

Is it possible to show/hide all matching lines in vi or vim? Not highlight but just show only that lines.

For example I have a text with ERROR word. How to show only lines containing ERROR and how to show only lines without ERROR ?

Is there a solution without deleting all matching lines and than just undoing this?

+7  A: 

You can use

:g/ERROR/

to print all the lines with ERROR

Also there is a Vim plugin which I saw many times but didn't use: foldsearch : fold away lines that don't match a given pattern

Mykola Golubyev
+1 for the link to the foldsearch plugin. It's unobtrusive and can come in handy.
+10  A: 

Do you know about the :global command? Does this do what you want?

:g/ERROR

and for the opposite

:g!/Error
or equivalent
:v/Error
A: 

Yes. :g/ERROR does the job, as everyone mentioned.

However, I could not find a solution to this problem that I frequently encounter. I suspect this is what you may be looking for too.

Is there a way to hide text while editing? For example, I have a latex document where I comment out huge sections (which I may need later, so I can not delete). However, while editing the document, I would not like to see those lines. I just want to see the lines without a comment symbol preceding them (or matching some regex). Is there a way to do this?

Pavan
Yes, you can fold the section. It will reduce it down to one line. There are several different folding modes - see the help system for more information (:h fold.txt)
Dave Kirby