[Tutorial] Remove the C/C++ comment block in Vim

Published: by Creative Commons Licence (Last updated: )

Remove the C comment block in Vim

For a multi-line comment block in C/C++, you can remove it in Vim by the following command:

:%s/\/\*\_.\{-}\*\///g

To remove line comments, you can use the following command:

:%s/\/\/.*$//g

Explanation:

  • \/\* — Starts the match with the beginning of a comment block /*.
  • \_.*\{-} — The \_.* pattern matches any character including newline (\_ extends . to match newlines), and \{-} makes it non-greedy.
  • \*\ / — Ends the match with the end of a comment block */.
  • /g — Applies the substitution globally across each line where a match is found.