简体   繁体   English

使用引导键对vim重新映射折叠导航

[英]vim remap fold navigation using leader key

I picked up this handy function for skipping up and down closed folds in vim: 我选择了这个方便的功能来跳过vim中的闭合折叠:

let mapleader = ","
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

As you can see, my leader key is set to , . 正如你所看到的,我的leader键设置为,

So now if I issue the command ,zj my cursor is moved to the next closed fold. 因此,现在如果我发出命令,zj我的光标将移至下一个关闭的折叠。 However, what I want is to have the zj command defaulted to moving to the next closed fold, and I want ,zj to move to the next fold (open or closed). 但是,我想要的是让zj命令默认移动到下一个折叠,而我想让,zj移动到下一个折叠(打开或关闭)。

What is the most elegant way to write the remapping so my vim behaves the way I want it to? 编写重新映射以使我的vim表现出我想要的方式的最优雅的方法是什么?

It sounds like you want this. 听起来像您想要这样。

nnoremap <silent> <leader>zj zj
nnoremap <silent> <leader>zk zk
nnoremap <silent> zj :call NextClosedFold('j')<cr>
nnoremap <silent> 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

nnoremap makes mapping non recursive so even if you redefine what zj and zk do you can always get back their default behavior. nnoremap使映射成为非递归的,因此,即使您重新定义zjzk ,也始终可以恢复其默认行为。 Then we just map zj and zk to the behavior you want. 然后,我们只需将zjzk映射到所需的行为即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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