Reputation: 14827
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?
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.
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; }
}
Upvotes: 52
Views: 41269
Reputation: 4504
Vim does not 'understand' your code enough to travel between functions, so the suggested solutions will not work perfectly.
The newer Helix editor (a modern vim alternative) has tree-sitter integration, which allows you to jump to functions / arguments / methods / classes.
Upvotes: 1
Reputation: 39
If you use taglist, I added a feature that does just that. You can jump from one tag to the other using Ctrl-up & Ctrl-down, provided the language is supported by taglist.
Here: https://github.com/man9ourah/taglist
and this to your .vimrc
.
nmap <silent> <c-up> <plug>(TlistJumpTagUp) " Map ctrl-up to move one tag up
nmap <silent> <c-down> <plug>(TlistJumpTagDown) " Map ctrl-down to move one tag down
Upvotes: 1
Reputation: 584
I spent hours to make this pattern: /^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{
, it works good for me.
EDIT: a better pattern(version 2): /\(\(if\|for\|while\|switch\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{
you can map some convenient bindings in your .vimrc, such as:
" 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):
" 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>
Upvotes: 14
Reputation: 138
Looks like a duplicate of: 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...)
Upvotes: 4
Reputation: 172658
Vim has [m
/ ]m
built in "for Java or similar structured language".
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.
Upvotes: 95