vimtricks.wiki Concise Vim tricks, one at a time.

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() {
    // ...
}
  • ]m from line 1 jumps to func helper() (line 5)
  • ]m again jumps to func cleanup() (line 9)
  • [m jumps back to func helper() (line 5)
  • ]M jumps to the closing } of the current method

Tips

  • These motions accept a count: 3]m jumps forward three methods
  • Combine with d or v to operate on methods: v]Md deletes 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

Next

How do I return to normal mode from absolutely any mode in Vim?