int main()
   printf("Hello world\n");

I want to comment line where the cursor is. For, example if I am on the line printf then I want to get following -

int main()
   /* printf("Hello world\n"); */

[keep in mind the comment style is /* */]

The command may be :C or :Comment or Ctrla key


1 Answer 1


One way would be to map the keystrokes you would use to a key combination:

nnoremap gcc I/* <ESC>A */<ESC>

In normal mode, when pressing gcc, this will go to the beginning of the line and insert /* , then goes to the end of the line and insert */.

This might be already enough for you, but if you want more I would recommend just using a plugin for that, e.g. Tim Pope's vim-commentary. Besides commenting C files, it also supports many other programming languages and uses the appropriate comment format for them. You can define custom comment formats, and you can also uncomment lines (with the same combination gcc).

  • This works and I accepted this. Can you provide uncomment solution using gcc?
    – alhelal
    Mar 26, 2018 at 1:38
  • 2
    @alhelal sure, use the mentioned plugin ;) Mar 26, 2018 at 7:04
  • 3
    And if you don't like vim-commentary for some reason, a couple of other popular commenting plugins you can try are NERD Commenter and TComment.
    – Rich
    Mar 26, 2018 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.