Is it possible to jump to closed folds in Vim?

我的梦境 提交于 2019-12-18 10:51:23

问题


In Vim, I frequently find myself wanting to do a quick zk or zj to jump to the previous or next fold in a file. The problem is, I frequently want to skip all the open folds, and just jump to the nearest closed fold.

Is there a way to do this? I see no built-in keymap in the manual.


回答1:


Let me propose the following mappings implementing the described behavior.

nnoremap <silent> <leader>zj :call NextClosedFold('j')<cr>
nnoremap <silent> <leader>zk :call NextClosedFold('k')<cr>
function! NextClosedFold(dir)
    let cmd = 'norm!z' . a:dir
    let view = winsaveview()
    let [l0, l, open] = [0, view.lnum, 1]
    while l != l0 && open
        exe cmd
        let [l0, l] = [l, line('.')]
        let open = foldclosed(l) < 0
    endwhile
    if open
        call winrestview(view)
    endif
endfunction



回答2:


No, there isn't (as far as I know) a build in method to do that. Interesting idea, though.

If I had some time at the moment, I might try to figure out a way to do it - unfortunatelly, being busy nowadays all I can suggest you is to look at the Detecting a folded line or an incremental search question (particularly the foldclosed function) and try to make a function yourself. Checking every line, if fold is open, skip ... something along those lines.



来源:https://stackoverflow.com/questions/9403098/is-it-possible-to-jump-to-closed-folds-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!