How do I jump to the next section or function definition in Vim?
Answer
]]
Explanation
The ]] motion jumps forward to the next line that starts with { in the first column, which is typically the beginning of a C-style function or section. This is incredibly useful for navigating large codebases where functions are separated by opening braces.
How it works
]]— jump forward to the next{in column 1[[— jump backward to the previous{in column 1][— jump forward to the next}in column 1[]— jump backward to the previous}in column 1
These motions work as standard Vim motions, so you can combine them with operators.
Example
Given this C file with the cursor on line 1:
void foo()
{
printf("hello");
}
void bar()
{
printf("world");
}
Pressing ]] moves the cursor to line 2 (the first {), and pressing ]] again moves to line 7 (the second {).
Tips
- Use
d]]to delete from the cursor to the next section boundary - In languages without column-1 braces, some filetype plugins remap
]]to jump between function definitions - Combine with
V]]to visually select entire sections