Hide all (not)matching lines in Vim

Viewed 57273

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?

8 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

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

in case you happen to use fzf you could use:

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

Some hackish dirty way to do this:

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