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 .
8 Answers
Go to [count] previous start of a method
Works for Java or similar structured languages, and for Python as well.

- 7,720
- 2
- 53
- 77

- 58,560
- 8
- 81
- 72
-
And for React/ES6/JavaScript class methods as well :-) – Chris Kobrzak Jan 02 '16 at 20:34
-
4But only works for methods, not functions (needs enclosing `class {}`). :-( Method specific question: http://stackoverflow.com/questions/12128678/vim-go-to-beginning-end-of-next-method – Ciro Santilli OurBigBook.com Apr 25 '17 at 14:57
-
Very nice. My precious. – daparic Dec 19 '19 at 18:36
-
1To add, for procedural languages like C the following combo will bring you to the beginning of the function `[m[{`, then you could map it to something like `[f`. – zazke Sep 17 '20 at 20:55
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
}

- 347,512
- 102
- 1,199
- 985
-
1
-
`[m` seems to work for me from inside an out-of-line method definition in C++... with no outer {} from a class definition or namespace. – dgrogan Nov 06 '18 at 21:29
-
@dgrogan on the example above, vim 8.0 Ubuntu 18.04 `-u NONE`, `]m` stops at closing braces. Things become worse if there are braces inside the function, e.g. `if {}`. – Ciro Santilli OurBigBook.com Nov 06 '18 at 21:46
-
1Alternatively, if the folding structure is defined in your file, `[z` / `]z` might work. – Hope Jun 26 '19 at 08:35
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.

- 4,745
- 37
- 43
-
-
1This does not jump to the begining of the function when you are inside nested `{}` blocks. `[m` is the way to got (think: go to last member) – Balint G. Oct 04 '22 at 10:08
-
-
-
-
@Geremia Ok. Maybe not vanilla? The doc for `[{` states _Go to previous unmatched '{'._. – Balint G. Oct 11 '22 at 09:03
-
@BalintG. That's what both `[{` and `[m` do for me, when I'm inside a `while {…}` which is inside a C function. It must consider a `while` statement a "method": "`[m Go to [count] previous start of a method (for Java or similar structured language).`" – Geremia Oct 11 '22 at 23:56
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>

- 564
- 1
- 7
- 13
-
Thanks works great for me! The only one I've found so far that even works with C++ namespaces – J-Win Jul 07 '19 at 16:17
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

- 100
- 1
- 5
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]

- 1
- 1

- 14,226
- 3
- 42
- 60
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

- 36,220
- 13
- 81
- 146
Searching (backwards) for ?^{
should normally get you there.

- 26,565
- 10
- 94
- 165
-
1Are there really "so many cases where this can go wrong"? The question specifically ask from "in the middle of function body", and when writing C code it should be a very non-standard coding style for a function not starting with '{' at the beginning of a line as well as having some line within the function also starting with '{'. – hlovdal Feb 08 '10 at 09:22
-
@vincentleest only if you are not inside a `{}` block already in C / C++. – Ciro Santilli OurBigBook.com May 08 '17 at 09:29