java Vim:转到下一个方法的开头/结尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12128678/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vim: Go to Beginning/End of Next Method
提问by void-pointer
Is there native functionality in Vim that allows one to move the cursor to the beginning/end of the next method? I already know about [[
, ]]
, []
, and ][
, but these don't cut the job, because they only work on braces that are in column zero. Hence, they are of little use in, say, navigating C++ code. Is there such a command that is already built into Vim? If not, would you recommend a plugin that does implement it?
Vim 中是否有允许将光标移动到下一个方法的开头/结尾的本机功能?我已经知道[[
, ]]
, []
, 和][
,但这些并没有减少工作,因为它们只适用于第零列中的大括号。因此,它们在导航 C++ 代码等方面用处不大。Vim 中是否已经内置了这样的命令?如果没有,您会推荐一个可以实现它的插件吗?
Thanks for your help!
谢谢你的帮助!
Edit: [{
and }]
will not work all of the time, because you have to be within the block with the {}
(and not in some deeper scope within that block) for you to end up at the right {
or }
afterwards.
编辑:[{
并且}]
不会一直工作,因为您必须在块内{}
(而不是在该块内的更深范围内)才能在右侧{
或}
之后结束。
Edit 2: Here's a code listing for which [m
and friends don't work.
编辑 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; }
}
回答by Ingo Karkat
Vim has [m
/ ]m
built in "for Java or similar structured language".
Vim 具有[m
/]m
内置“用于 Java 或类似的结构化语言”。
I have written custom versions that handle Vim functions, VBScript, and batch files, among others. These are all powered by my CountJump plugin, which can be used to write custom jump functions based on regular expressions.
我编写了处理Vim 函数、VBScript和批处理文件等的自定义版本。这些都由我的CountJump 插件提供支持,该插件可用于编写基于正则表达式的自定义跳转函数。
回答by user765572
Looks like a duplicate of: Vim [m motion with c#
看起来像以下内容的副本:Vim [m motion with c#
You could, for instance, give a try to this dirty trick: 9]}
.
Which just jumps to the 9-th }
from the current location (if you're not too nested, should work...)
你可以,比如,给一个尝试这种肮脏的伎俩:9]}
。它只是}
从当前位置跳到第 9 个(如果你不是太嵌套,应该工作......)
回答by zhou lin
I spent hours to make this pattern: /^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{
, it works good for me.
我花了几个小时来制作这个图案:/^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{
,它对我很有用。
EDIT: a better pattern(version 2): /\(\(if\|for\|while\|switch\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{
编辑:更好的模式(版本 2):/\(\(if\|for\|while\|switch\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{
you can map some convenient bindingsin your .vimrc, such as:
您可以在 .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>
EDIT: a better pattern(version 2):
编辑:更好的模式(版本 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>