我希望能够使用 Vim 在函数之间导航光标。主要是,我想要一个命令来允许我转到下一个功能,例如
}
允许我转到下一段。我发现了这个:
不幸的是,它在 JavaScript 中似乎运行得不太好。另外,我相信它也不适用于 Python,因为 Python 不太依赖 { 和 }。
有什么帮助吗?
在Python文件中我发现:
}
将带我到街区的尽头,]]
将带我开始下一个功能。[[
将我带到当前功能的开始,或者如果我继续按,则进入上面的功能。]}
不过好像没用。
我相信标准的 vim 动作将在 javascript 中为你工作。我认为 $VIMRUNTIME/ftplugin/javascript.vim 文件定义了函数/类/等边界。
参见
:h ]m
:
*]m*
]m Go to [count] next start of a method (for Java or
similar structured language). When not before the
start of a method, jump to the start or end of the
class. When no '{' is found after the cursor, this is
an error. |exclusive| motion.
*]M*
]M Go to [count] next end of a method (for Java or
similar structured language). When not before the end
of a method, jump to the start or end of the class.
When no '}' is found after the cursor, this is an
error. |exclusive| motion.
*[m*
[m Go to [count] previous start of a method (for Java or
similar structured language). When not after the
start of a method, jump to the start or end of the
class. When no '{' is found before the cursor this is
an error. |exclusive| motion.
*[M*
[M Go to [count] previous end of a method (for Java or
similar structured language). When not after the
end of a method, jump to the start or end of the
class. When no '}' is found before the cursor this is
an error. |exclusive| motion.
更精确的 JS 动作:https://github.com/okcompute/vim-javascript-motions
对于 python,请参阅 $VIMRUNTIME/ftplugin/python.vim:
execute "nnoremap <silent> <buffer> ]] :call <SID>Python_jump('n', '". b:next_toplevel."', 'W', v:count1)<cr>"
execute "nnoremap <silent> <buffer> [[ :call <SID>Python_jump('n', '". b:prev_toplevel."', 'Wb', v:count1)<cr>"
execute "nnoremap <silent> <buffer> ][ :call <SID>Python_jump('n', '". b:next_endtoplevel."', 'W', v:count1, 0)<cr>"
execute "nnoremap <silent> <buffer> [] :call <SID>Python_jump('n', '". b:prev_endtoplevel."', 'Wb', v:count1, 0)<cr>"
execute "nnoremap <silent> <buffer> ]m :call <SID>Python_jump('n', '". b:next."', 'W', v:count1)<cr>"
execute "nnoremap <silent> <buffer> [m :call <SID>Python_jump('n', '". b:prev."', 'Wb', v:count1)<cr>"
execute "nnoremap <silent> <buffer> ]M :call <SID>Python_jump('n', '". b:next_end."', 'W', v:count1, 0)<cr>"
execute "nnoremap <silent> <buffer> [M :call <SID>Python_jump('n', '". b:prev_end."', 'Wb', v:count1, 0)<cr>"