How do I quickly jump between function or method definitions in code?
Answer
]] / [[
Explanation
The ]] and [[ commands let you jump forward and backward between section boundaries, which in most programming languages correspond to function or method definitions. They look for opening curly braces { in the first column, making them a fast way to hop between top-level blocks in C-style languages.
How it works
]]jumps forward to the next{in column 1 (typically the start of the next function body)[[jumps backward to the previous{in column 1][jumps forward to the next}in column 1 (the end of the next function body)[]jumps backward to the previous}in column 1 (the end of the previous function body)
Example
Given a C file:
void foo() {
// ...
}
void bar() {
// ...
}
void baz() {
// ...
}
With the cursor inside foo(), pressing ]] jumps to the opening { of bar(). Pressing ]] again jumps to baz(). Press [[ to go back to bar().
Tips
- These commands work best in C, Go, Rust, and other languages where
{appears in column 1 for function definitions - In Python, Vim's filetype plugin remaps
]]and[[to jump betweenclassanddefkeywords instead - Use
]mand[mas alternatives — they jump to the start of the next/previous method (works with Java-style indented braces) ]Mand[Mjump to the end of the next/previous method- Combine with operators:
d]]deletes from the cursor to the next function boundary,y[[yanks back to the previous one - Add a count for bigger leaps:
3]]jumps forward three function definitions - These motions set a jump mark, so
<C-o>returns you to where you started