Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide all (not)matching lines in Vim

Tags:

vim

vi

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

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

Is there a solution without deleting all matching lines and then just undoing it?

like image 992
Alex Bolotov Avatar asked May 14 '09 12:05

Alex Bolotov


People also ask

How do I get rid of matching lines in vim?

In order to delete lines matching a pattern in a file using vim editor, you can use ex command, g in combination with d command.

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.

How do I delete all lines starting with vi?

To remove all lines containing a particular string in the vi or Vim text editors, you can use the g command to globally search for the specified string and then, by putting a "d" at the end of the command line, specify that you want all lines containing the specified string deleted.

How do you select multiple lines to delete in vim?

Press 'v' to enter a select mode, and use arrow keys to move that around. To delete, press x. To select lines at a time, press shift+v.


2 Answers

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

:g/ERROR 

and for the opposite:

:g!/Error 

or equivalently:

:v/Error 
like image 128
user55400 Avatar answered Nov 02 '22 23:11

user55400


Another approach depending on your use case would be using vimgrep and its results in quickfix. You can do the following:

:vimgrep pattern % will search the current file and take you to the first search result. More importantly it also puts the results in the "quickfix list".

:copen will then open the quickfix list in a separate quickfix-window. So you will have a separate window with all lines from your last vimgrep. Inside the quickfix-window you can then hit Enter or double-click on a line to jump to the corresponding line in your original file.

:colder will let you go back to older quickfix lists (older vimgrep results). And :cnewer goes forward to newer search results.

Note that the quickfix list is also updated when running :make (which is why its called quickfix for fixing errors). Because of this there also is an alterative to the quickfix list called the "location list". To use it instead you use :lvimgrep, then use l-prefixed commands rather than c-prefixed commands - :lopen, :lolder, :lnewer.

There is, of course, a lot more you can do. See :help quickfix for more info.

PS, You said you didn't want an approach that deletes lines and then undoing them. But since you marked g/ERRORas the answer I thought I would point out a quick and dirty way is to do g!/ERROR/d. You can then easily undo it using u. Also FYI, you can do :set hlsearch to highlight patterns matched with :g commands.

like image 20
studgeek Avatar answered Nov 03 '22 00:11

studgeek