Vim Tips Wiki
Tip 389 Printable Monobook Previous Next

created 2002 · complexity basic · version 6.0

Folds are useful to temporarily hide text that you don't currently need to see. By default, when you search for text, each fold containing a search hit automatically opens when you find the hit. This behavior is controlled with the 'foldopen' option (abbreviated to 'fdo').

To search only in open folds (unfolded text):

:set fdo-=search

With the above, a search shows one hit per fold that contains the search target. The fold is not opened, and is only found once, even if it contains several instances of the search target.

You can also perform commands only on lines in open folds. For example, the following command will change every 'old' to 'new', but only in lines that are not folded:

:folddoopen s/old/new/ge

With :folddoopen, each unfolded line is marked. Then the command (s/old/new/ge) is executed on each marked line. Therefore the s/// command has no range (it operates on the current line), and it has the e flag so no error is shown if a line does not contain 'old'.



I'm seeing some weird messages from Vim 7.2 when using the above to s/old/new/ in text with several closed folds. The command works, but it shows a spurious "x substitutions on n lines" message (x and n are too big). Any thoughts? --JohnBeckett 00:05, 14 October 2008 (UTC)