How to convert C89 style comments to C99

Mario Emmanuel
The Console
Published in
Jun 5, 2021
Image from Pixabay

Three regexp replace operations in Vim suffice to convert C89 into C99/C++ style comments.

1. Remove */
%s/\*\///
2. Replace /* by //
%s/\/\*/\/\//
3. Replace <SPACE>*<SPACE> by //<SPACE>
%s/ \* /\/\/ /

You can put that inside a function in your home .vimrc file if you need to refactor several source files:

" CONVERT C89 TO C99 COMMENTS                                                                                                        
func! C89toC99()
%s/\*\///
%s/\/\*/\/\//
%s/ \* /\/\/ /
endfunc

And then invoke it in vim:

call C89toC99()

Despite it is not a fully tested conversion method (it will let some blank lines here and there and it might not capture all your inner * characters it will still likely save you 95% of the required time to do the change manually.

--

--