95

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?

1

9 Answers 9

117

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
4
  • 22
    This is not very good. Once you scroll to the bottom, then the command finishes and everything show up again. It is not really hiding those contents.
    – Kan Li
    Jul 11, 2013 at 22:13
  • @KanLi hit g< to see again, or @: if you havent run and commands. Jul 24, 2019 at 16:33
  • 2
    you can delete :v/Error /d to get back undo "u"
    – toshi
    Jun 15, 2021 at 8:34
  • Is it possible to preserve syntax highlights from the original buffer in that "filtered window" after :g/pattern/?
    – bloody
    Nov 30, 2021 at 22:24
55

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.

2
  • 1
    Thanks for the tip on ":g!/ERROR/d" better than the accepted answer.
    – VinGarcia
    Dec 13, 2017 at 13:41
  • 1
    Can you do this with lines not matching the pattern?
    – Jacktose
    Jul 7, 2020 at 18:46
41

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

1
  • 16
    +1 for the link to the foldsearch plugin. It's unobtrusive and can come in handy.
    – user55400
    May 14, 2009 at 13:54
31

The best way to do this is->

:vimgrep /something/g % | copen

This will open the list of matches for your keyword and also will show only the matched lines in quickfix window.

Replace % with path to file if not considering the current file.

2
  • 1
    Replace % with the file path if you are not considering the current file.
    – Shivendra
    Sep 6, 2015 at 7:12
  • Can you do this with lines not matching the pattern?
    – Jacktose
    Jul 7, 2020 at 18:46
3

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

1
  • This is exactly another answer on this question.
    – Shivendra
    Oct 28, 2019 at 23:42
3

in case you happen to use fzf you could use:

  • :Lines in all open files
  • :BLines only in open buffer
  • :Rg [pattern] using ripgrep
1

You probably mean command in less vi vim & /pattern/ which shows lines containing /pattern/ (like grep).

1

Some hackish dirty way to do this:

:w (save)
ggdG (deletes everything)
:.!grep something % (replace current line with grep output)
0

then you can just do

:g!/<pattern>/d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.