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

How do I jump to the start or end of a C-style block comment in Vim?

Answer

[/

Explanation

When editing code, you often need to navigate to the boundaries of multi-line block comments (/* ... */). Vim provides two dedicated motions: [/ to jump to the start of the nearest enclosing or preceding /* comment, and ]/ to jump to the end of the nearest following */.

How it works

  • [/ — moves the cursor backward to the opening /* of a C comment block
  • ]/ — moves the cursor forward to the closing */ of a C comment block

These are part of Vim's built-in [ and ] bracket motion family (:help [/). They work on C-style /* */ comments and are available without any plugins.

Both motions also accept a count prefix to jump multiple comment boundaries:

2[/   " jump to the start of the 2nd preceding comment
3]/   " jump past the 3rd following comment end

Example

int main() {
    /* This is a
       multi-line comment */       <- cursor here
    return 0;
}

With cursor anywhere inside or after the comment:

  • [/ moves to the /* on the second line
  • ]/ moves to the */ closing the comment

Tips

  • Works as a motion with operators: d]/ deletes from the cursor to the end of the comment
  • Combine with v[/ to visually select from cursor back to the comment's opening
  • Similar motions exist for C preprocessor: use [# / ]# to jump between matching #if/#endif directives

Next

How do I make Vim automatically reformat paragraphs as I type so lines stay within the textwidth?