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

How do I jump to the start or end of a C-style block comment from anywhere inside or near it?

Answer

[* and ]*

Explanation

The [* and ]* motions (also written as [/ and ]/) let you jump directly to the opening /* or closing */ of a C-style block comment. They work from anywhere — inside the comment, before it, or after it — making it easy to navigate around large doc-comment blocks without manual searching.

How it works

  • [* (or [/) — jump backward to the start of the nearest /* ... */ comment
  • ]* (or ]/) — jump forward to the end of the nearest /* ... */ comment

These are built-in Vim motions documented under :help [*.

Example

Given a C file with:

/* This is a long
   block comment
   spanning several lines */
int main() { ... }

With the cursor anywhere on line 2 ( block comment), pressing [* jumps to the /* on line 1, and ]* jumps to */ on line 3.

Tips

  • Combine with % to jump between the /* and */ if your 'matchpairs' includes them.
  • Works in any filetype — Vim matches /* and */ literally, so it is not limited to C.
  • Use with operators: d]* deletes from cursor to end of block comment.
  • [/ and ]/ are synonyms for [* and ]* respectively.

Next

How do I open the directory containing the current file in netrw from within Vim?