45

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; }

}
void-pointer
  • 14,247
  • 11
  • 43
  • 61
  • @EitanT I should have mentioned that those won't work all the time either, because you have to be *inside* the scope of the thing with the `{}`. – void-pointer Aug 26 '12 at 08:59
  • @PeterLawrey If that were true, many (dare I say most) people using Vim would no longer be using Vim =) – void-pointer Aug 26 '12 at 09:00
  • 11
    What about `]m`, `]M`, `[m` and `]m`? – Eitan T Aug 26 '12 at 09:01
  • @Eitan1 Added a code listing so that we can be explicit about the behavior of the commands. – void-pointer Aug 26 '12 at 09:14
  • @void-pointer I used to use vim only for editing and I wasn't convinced an IDE would make any difference. Eight years ago I switched to using an IDE and when you look at the built-in functionality an IDE gives you today, I can only conclude some people prefer to do things the hard way (admittedly I was one of them) – Peter Lawrey Aug 26 '12 at 09:35
  • @PeterLawrey I would be fine with using an IDE if I could still have my Vim navigation and autocommand functionality working. What do you suggest for C++ development on OS X? I agree that C++ and some other languages are sufficiently complex that navigation using Vim's level of intelligence is not very good. – void-pointer Aug 26 '12 at 10:03
  • 1
    I am not as familiar with C++, but I have used eclipse. It has a vim plug-in so which is supposed to give you much of the functionality. I find with an IDE much of the need to navigate goes away. e.g. In Java if you use a class, a field or a method which hasn't been imported/defined it will add it in your code without having to scroll to where it goes, add it and scroll back. If you want to move a class between packages, you can drag and drop all the references (even comments, XML and text file references) can be changed. `+` jumps to a declaration of something. – Peter Lawrey Aug 26 '12 at 10:08
  • @PeterLawrey There's something called [Eclim](http://eclim.org) that basically runs a headless Eclipse inside Vim; I'll check that out when I get time. – void-pointer Aug 26 '12 at 10:14
  • @EitanT what do exactly these shortcuts do? I just tried them out and it stops sometimes at spacing lines inside a method, other it jumps between methods... – User Feb 13 '15 at 16:03

5 Answers5

88

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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 2
    I think support for C++ will require something more powerful than regular expressions, because even `[m`, `]m`, `[M`, `]M` are not working for my C++ code. – void-pointer Aug 26 '12 at 09:12
  • 1
    If you're after a 100%-solution, you need a full C++ parser; but usually, a "mostly correct" solution can often be achieved with regexps; cp. Vim's syntax highlighting. – Ingo Karkat Aug 26 '12 at 11:58
  • wow! never knew this motion existed. It works great with typescript/javascript on neovim. I am very happy to discover this built-in vim motion. So cool – avimehenwal Sep 26 '22 at 22:33
13

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{

see the effect here: enter image description here enter image description here

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>
ZHOU Ling
  • 564
  • 1
  • 7
  • 13
  • Thanks for including full details, including the `noremap` command using `search`. I had a different issue, but your example cleared it up for me. – Jeff Learman Feb 02 '23 at 14:08
4

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...)

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
user765572
  • 138
  • 5
1

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.

joepio
  • 4,266
  • 2
  • 17
  • 19
0

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
Mansour
  • 31
  • 1
  • 6