如何在Vim中跳转到当前函数体的开头?

Hai*_*ang 46 vim

作为标题,如果我在函数体的中间并且函数体很长,我怎样才能跳回到函数体的开头.

ax.*_*ax. 61

[m

转到[count]上一个方法的开始

适用于Java或类似的结构化语言,也适用于Python.

  • 但仅适用于方法,不适用于函数(需要包含 `class {}`)。:-( 方法特定问题:http://stackoverflow.com/questions/12128678/vim-go-to-beginning-end-of-next-method (4认同)

Cir*_*四事件 21

C语言 [[

如果您的C代码是非埃及风格:

[[
Run Code Online (Sandbox Code Playgroud)

[m只有{}在函数周围有用时才有效,例如class { method(){} }Java/C++.

这是一个适用于埃及和非埃及大括号的好赌注:

?^[^ \t#]
Run Code Online (Sandbox Code Playgroud)

例子:

void egypt() {
#define DONTCARE 1
    int indented code = 1;
}

void tpyge()
{
#define DONTCARE 1
    int indented code = 1
} 
Run Code Online (Sandbox Code Playgroud)


Ger*_*mia 11

对于包含在一对花括号 {} 中的函数:

跳转到开头: [{

跳转到结尾: ]}

对于使用这些函数的括号或方括号替换 curl-blackets。


zho*_*lin 8

我花了几个小时来制作这个图案:/^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{,它对我很有用。

编辑:更好的模式(版本 2):/\(\(if\|for\|while\|switch\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{

看这里的效果: 在此处输入图片说明

您可以在 .vimrc 中映射一些方便的绑定,例如:

" jump to the previous function
nnoremap <silent> [f :call search('^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{', "bw")<CR>
" jump to the next function
nnoremap <silent> ]f :call search('^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{', "w")<CR>
Run Code Online (Sandbox Code Playgroud)

编辑:更好的模式(版本 2):

" jump to the previous function
nnoremap <silent> [f :call
\ search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{', "bw")<CR>
" jump to the next function
nnoremap <silent> ]f :call
\ search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{', "w")<CR>
Run Code Online (Sandbox Code Playgroud)


Bac*_* Hu 5

2022年,护树人值得您关注。

内置函数[m使用词法规则,并且总是跳转到前一个{位置或最外层{位置。

相比之下,treesitter利用了句法信息,因此可以跳转到更精确的位置,并且没有如下所述的限制:

上面两个命令假设该文件包含一个带有方法的类。类定义被“{”和“}”包围。类中的每个方法 类中的每个方法也用“{”和“}”包围。这适用于 Java 语言。文件看起来像这样:>

如果您想了解更多信息,请查看https://github.com/nvim-treesitter/nvim-treesitter-textobjects