Haiyuan Zhang
Haiyuan Zhang

Reputation: 42852

How to jump to the beginning of the current function body in Vim?

As title, if I'm in the middle of function body and the function body is very long, how can I jump back to the beginning of the function body .

Upvotes: 91

Views: 45201

Answers (8)

Bachelar Hu
Bachelar Hu

Reputation: 150

In 2022, treesitter deserves your attention.

The built-in [m uses lexical rules and always jumps to the previous { position or the outermost { position.

In contrast, treesitter takes advantage of syntactic information, so it can jump to a more precise position, and there are no limitations as described below:

The above two commands assume that the file contains a class with methods. The class definition is surrounded in '{' and '}'. Each method in the class Each method in the class is also surrounded with '{' and '}'. This applies to the Java language. file looks like this: >

If you want to learn more, check https://github.com/nvim-treesitter/nvim-treesitter-textobjects

Upvotes: 6

Geremia
Geremia

Reputation: 5682

For functions contained in a pair of curly-braces {}:

Jump to beginning: [{

Jump to end: ]}

Replace the curly-blackets by parens or square-brackets for functions that use those.

Upvotes: 45

ax.
ax.

Reputation: 60217

[m

Go to [count] previous start of a method

Works for Java or similar structured languages, and for Python as well.

Upvotes: 120

ZHOU Ling
ZHOU Ling

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{

see the effect 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>

Upvotes: 9

C language [[

If your C code is in the non-Egyptian style:

[[

[m only works if you have an enclosing {} around the function, e.g. class { method(){} } for Java / C++.

And this is a good bet that works for both Egyptian and non-Egyptian braces:

?^[^ \t#]

Examples:

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

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

Upvotes: 48

Rob Wells
Rob Wells

Reputation: 37157

Once you've got moving around blocks and paragraphs in code sorted you might like to look at what you can do when you're in the middle of those blocks by looking at this part of the vim doc's.

Things like delete the block, insert before the block, append after the block, etc.

HTH

Upvotes: 0

KevinDTimm
KevinDTimm

Reputation: 14376

BTW, the only relatively sure way to be able to do this is to modify vim, see this post

[edit]
and this only works with languages supported by exuberant ctags. Since we've not been deigned fit to know which language you wish to do this in, it's possible that this answer will not be correct either.
[/edit]

Upvotes: 1

hlovdal
hlovdal

Reputation: 28268

Searching (backwards) for ?^{ should normally get you there.

Upvotes: -1

Related Questions