yolenoyer
yolenoyer

Reputation: 9455

Vim: Is it possible to have a filtered view of a file?

In a given text file, I would like to work on a "filtered view" (hiding lines with a pattern), but still to be able to edit visible lines : the filtering would only affect the visibility of some lines, and as soon as I would reset the filter, the hidden lines would appear again.

The feature I describe could be compared to the & key in the less command (except, of course, that less can't edit the file's content) :

Does such a feature exist in vim, natively or as a plugin?

Upvotes: 4

Views: 1240

Answers (2)

Ingo Karkat
Ingo Karkat

Reputation: 172738

This usually is done with folding; you can easily define a 'foldexpr' that filters lines based on a regular expression match; see Folding with Regular Expression for implementations.

However, a single fold line will remain for each condensed block. To do away with those, I can only think of the NrrwRgn plugin, which transfers selected lines into a separate scratch buffer. It's usually only for a single block, but :help NR-multi-example suggests this works on ranges, too:

For example before editing your config file, you decide to strip all comments for making big changes but when you write your changes back, these comments will stay in your file. You would do it like this: >

 :v/^#/NRP
 :NRMulti

Upvotes: 2

lcd047
lcd047

Reputation: 5861

It can be done with plain Vim, and it's named folding. Drew Neil has two screencasts about it that you might find informative.

Upvotes: 2

Related Questions