Hide all (not) matching strings in Vim

Is it possible to show / hide all relevant lines in vi or Vim? Do not select, but just show only those lines.

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

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

+81
vim vi
May 14, '09 at 12:10
source share
7 answers

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

 :g/ERROR 

and for the opposite:

 :g!/Error 

or equivalently:

 :v/Error 
+95
May 14 '09 at 12:14
source share
β€” -

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

:vimgrep pattern % will search for the current file and bring you to the first search result. More importantly, it also puts the results on the quickfix list.

:copen will open the quick delete list in a separate quick fix window. This way you will have a separate window with all the lines from your last vimgrep. In the quickfix window, you can press Enter or double-click on a line to go to the corresponding line in the source file.

:colder will allow you to revert to old fastfix lists (older vimgrep results). And :cnewer moving on to newer search results.

Please note that the quickfix list is also updated at startup :make (which is why it is called quickfix for error correction). Because of this, there is also an alternative to the quick fix list called the β€œlocation list”. Instead, you use :lvimgrep , then use the l-prefixed commands, not the c-prefixed - :lopen :lolder :lnewer .

There is, of course, much more that you can do. See :help quickfix .

PS, You said that you do not need an approach that deletes lines and then cancels them. But since you marked g/ERROR as the answer, I thought I would point out a quick and dirty way to do g!/ERROR/d . Then you can easily undo it using u . You can also do FYI :set hlsearch to highlight patterns matching the commands :g .

+49
Sep 18 '12 at 23:26
source share

you can use

 :g/ERROR/ 

to print all lines using ERROR

There is also a Vim plugin that I saw many times, but did not use: foldsearch: fold lines that do not match the given pattern

+37
May 14, '09 at 12:13
source share

The best way to do it β†’

 :vimgrep /something/g % | copen 

This will open a match list for your keyword, and also display only matching lines in the quick delete window.

+22
Sep 06 '15 at 7:11
source share

:vimgrep/something/g % | copen :vimgrep/something/g % | copen :vimgrep/something/g % | copen :vimgrep/something/g % | copen works amazingly. Also :g/<pattern>/d can be used to delete lines with a pattern

+2
Oct 12 '18 at 18:25
source share

You probably mean a command in less than vi vim and / pattern / which shows lines containing / pattern / (e.g. grep).

0
May 28 '15 at 7:26
source share

in case you use fzf you can use:

  • :Lines in all open files
  • :BLines only in open buffer
  • :Rg [pattern] using ripgrep
0
Jul 24 '19 at 12:01
source share



All Articles