Vim Tips Wiki
Register
Advertisement
Tip 35 Printable Monobook Previous Next

created March 7, 2001 · complexity intermediate · author slimzhao · version 5.7


With the following mappings, you can press \c to change all C++ comments to C comments, or press \C to change all C comments to C++ comments (assuming you are using backslash for your <leader> key).

Note: The mappings only handle single-line C comments.

For example, if you press \c then the following C++ code:

int i = 12;    // this is a comment
char c = 'A';  // and here is another comment

changes to use C comments:

int i = 12;    /* this is a comment */
char c = 'A';  /* and here is another comment */

Here are the mappings. You could put these in your vimrc:

" C++ //-comment to C /*-comment-*/
:noremap <leader>c :%s://\(.*\):/*\1 */:<CR>

" C /*-single-line-*/ to C++ //-comment
:noremap <leader>C :%s:/\*\(.\{-\}\)\s*\*/://\1:<CR>

Comments


Advertisement