Vim Tips Wiki
m (Search only in unfold text(intend to work with diff) moved to Search only in unfolded text: Page moved by JohnBot to improve title)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=389
 
|id=389
  +
|previous=388
|title=search only in unfold text(intend to work with diff)
 
  +
|next=390
|created=December 17, 2002 12:56
+
|created=2002
 
|complexity=basic
 
|complexity=basic
|author=Demai Ni
+
|author=
 
|version=6.0
 
|version=6.0
 
|rating=0/0
 
|rating=0/0
  +
|category1=Folding
|text=
 
 
|category2=Searching
Sometimes I would like to search/replace the code in the latest version. That is when I show diff between two version of code, I would like to only search the unfold. The following function may do the replace job:
 
 
}}
  +
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 <code>'foldopen'</code> option (abbreviated to <code>'fdo'</code>).
   
  +
To search only in open folds (unfolded text):
 
<pre>
 
<pre>
  +
:set fdo-=search
function Foldrepl(spattern, tpattern)
 
  +
</pre>
normal gg "go to top of the file
 
if &diff "need to change fold option for diff
 
exec "set diffopt=context:0"
 
endif
 
"echo a:spattern
 
"echo a:tpattern
 
let mycount =0
 
while search(a:spattern, "W") > 0 "find the search pattern
 
if foldlevel(line(".")) < 1 "not in flod
 
exec "s/".a:spattern."/".a:tpattern."/g"
 
let mycount = mycount + 1
 
endif
 
endwhile
 
   
  +
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.
if &diff "need to restore fold option, mine is 4
 
  +
set diffopt=context:4
 
  +
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:
endif
 
  +
<pre>
echo mycount ." lines are changed"
 
  +
:folddoopen s/old/new/ge
endfunction
 
 
</pre>
 
</pre>
   
  +
With <code>:folddoopen</code>, each unfolded line is marked. Then the command (<code>s/old/new/ge</code>) is executed on each marked line. Therefore the <code>s///</code> command has no range (it operates on the current line), and it has the <code>e</code> flag so no error is shown if a line does not contain 'old'.
   
  +
==References==
It can be changed to do the search job or both.
 
  +
*{{help|folding}}
}}
 
  +
*{{help|'foldopen'}}
  +
*{{help|:folddoopen}}
  +
*{{help|:folddoclosed}}
  +
 
==Comments==
  +
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? --[[User:JohnBeckett|JohnBeckett]] 00:05, 14 October 2008 (UTC)
   
  +
----
== Comments ==
 
[[Category:Searching]]
 

Latest revision as of 05:28, 13 July 2012

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'.

References[]

Comments[]

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)