Vim:转到下一个方法的开始/结束

voi*_*ter 23 c++ java methods vim

Vim中是否有本机功能允许用户将光标移动到下一个方法的开头/结尾?我已经知道了[[,]],[],和][,但这些不切的工作,因为他们只对处于零柱支撑工作.因此,它们在导航C++代码方面几乎没有用处.是否有这样的命令已经内置到Vim中?如果没有,你会推荐一个实现它的插件吗?

谢谢你的帮助!

编辑:[{并且}]不会一直工作,因为你必须在块内{}(并且不在该块内的某个更深的范围内),以便最终在右边{}之后.

编辑2:这是一个代码清单,其中[m和朋友不工作.

namespace foo {

#define define_foo         \
    template <class T>     \
    struct foo_traits<X>   \
    {                      \
        using foo = X;     \
    };

template <class T>
struct foo_traits;

define_bar(T*, T*, T*);

template <class T>
struct baz;

template <class T>
struct baz<T&>
{
    static T* apply(T& t) { return &t; }
};

template <class T>
inline T a(T t) { return t; }

}
Run Code Online (Sandbox Code Playgroud)

Ing*_*kat 49

Vim已经[m/ ]m内置了"用于Java或类似的结构化语言".

我编写了自定义版本来处理Vim函数,VBScript批处理文件等.这些都是由我的CountJump插件提供的,可以用来根据正则表达式编写自定义跳转函数.

  • 我认为对 C++ 的支持需要比正则表达式更强大的东西,因为即使`[m`、`]m`、`[M`、`]M` 也不适用于我的 C++ 代码。 (2认同)

zho*_*lin 7

我花了几个小时来制作这个图案:/^\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)