Vim Tips Wiki
m (Reformat, categorize)
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)

Revision as of 09:28, 18 October 2007

Previous TipNext Tip

Tip: #389 - Search only in unfolded text

Created: December 17, 2002 12:56 Complexity: basic Author: Demai Ni Version: 6.0 Karma: 0/0 Imported from: Tip#389

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:

function Foldrepl(spattern, tpattern)
    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        

    if &diff            "need to restore fold option, mine is 4
        set diffopt=context:4
    endif    
    echo mycount ." lines are changed"
endfunction 


It can be changed to do the search job or both.

Comments