How do I jump to the start or end of a C-style comment block?
Answer
[/ and ]/
Explanation
Vim provides two motions for navigating C-style block comments (/* ... */): [/ moves the cursor to the start of the previous /* comment, and ]/ moves to the end of the next */ comment. These are particularly useful when working in C, C++, Java, or any language with block comments.
How it works
[/— Move backward to the nearest/*(comment open)]/— Move forward to the nearest*/(comment close)- Both accept a count prefix:
2[/jumps to the second previous comment start - The motions work in Normal and Visual mode, allowing you to select entire comment blocks
Example
Given this C code with cursor anywhere on line 5:
int add(int a, int b) {
/* This function adds
two integers and
returns the result. */
return a + b;
}
Pressing [/ moves the cursor to the /* on line 2. Pressing ]/ from there moves to the */ on line 4.
To visually select the entire comment block from outside it, use [/v]/ — jump to the start, enter Visual mode, then extend to the end.
Tips
[*and]*are aliases for[/and]/respectively- To delete a comment block: position before it, then
d]/deletes from cursor to the end of the next comment - These motions only recognise
/* */style comments, not//line comments - Use
:help [/or:help ]/*to confirm behaviour for your Vim version