How do I jump between method or function definitions in code?
Answer
]m / [m / ]M / [M
Explanation
When navigating large source files, ]m and [m let you jump directly to the start of the next or previous method. These motions understand the brace-delimited structure of languages like Java, C, Go, and JavaScript, making it fast to hop between functions without scrolling or searching.
How it works
]m— jump to the start of the next method (next{at the beginning of a block)[m— jump to the start of the previous method]M— jump to the end of the next method (next closing})[M— jump to the end of the previous method
These motions look for { and } characters that appear at the start of a line or after a method signature, following the conventional code structure.
Example
Given this code with the cursor on line 1:
func main() {
fmt.Println("hello")
}
func helper() {
// ...
}
func cleanup() {
// ...
}
]mfrom line 1 jumps tofunc helper()(line 5)]magain jumps tofunc cleanup()(line 9)[mjumps back tofunc helper()(line 5)]Mjumps to the closing}of the current method
Tips
- These motions accept a count:
3]mjumps forward three methods - Combine with
dorvto operate on methods:v]Mddeletes from cursor to the end of the next method - For C-style languages,
]]and[[jump to the next/previous{in column 1, which often corresponds to function boundaries - Works best with languages that use
{}delimiters; for Python, consider using treesitter-based navigation instead