简体   繁体   中英

How can I tell Vim to close only a specific level of folds in a file?

For example, how can I tell Vim to only close all level 2 folds in a file, leaving other levels open?

Edit: Let's say I have 3 levels of folds in my file. 1, 2, and 3. I only want to close level 2 folds, leaving levels 1 and 3 open.

You can also set 'foldlevel' via the [count] on zM : 2zM ; this is shorter than @timrau's answer.

使用foldlevel

:set foldlevel=2
function! FoldToTheLevel(TheLevel)    
    "set mark "f" at current position                   
    execute "normal! mf"
    "close all the folder recursively
    execute "normal! ggVGzC" 

    "open all the folder which have smaller level 
    let h=0  
    while h<a:TheLevel              
        execute "normal! ggVGzo"  
        let h+=1                                                                     
    endwhile 

    "open all the folder which have larger level           
    folddoclosed execute "normal! VzOzc" 
    "jump back and show in the middle
    execute "normal! `fzz"
endfunction   

command! -nargs=1 F call FoldToTheLevel(<f-args>)    

add this to .vimrc

run

:F num

to close the folder which the level is {num}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM