问题描述
在 Vim 中,我经常发现自己想做一个快速的 或 来跳转到文件中的上一个或下一个折叠.问题是,我经常想跳过所有打开的折叠,而只是跳到最近的关闭折叠.
In Vim, I often find myself wanting to do a quick or 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 help.
推荐答案
让我提出描述行为的以下实现.
Let me propose the following implementation of 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
如果映射需要接受数量的计数相应动作的重复次数,可以实现用于重复任何给定命令的简单函数:
If it is desirable for the mappings to accept a count for the numberof repetitions of the corresponding movement, one can implementa simple function for repeating any given command:
function! RepeatCmd(cmd) range abort
let n = v:count < 1 ? 1 : v:count
while n > 0
exe a:cmd
let n -= 1
endwhile
endfunction
然后重新定义上面的映射如下:
and then redefine the above mappings as follows:
nnoremap <silent> <leader>zj :<c-u>call RepeatCmd('call NextClosedFold("j")')<cr>
nnoremap <silent> <leader>zk :<c-u>call RepeatCmd('call NextClosedFold("k")')<cr>
这篇关于是否可以在 Vim 中跳转到下一个关闭的折叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!