0
#inlcude<stdio.h>
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 -

#inlcude<stdio.h>
int main()
{
   /* printf("Hello world\n"); */
} 

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

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

6

One way would be to simple 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 '18 at 1:38
  • 2
    @alhelal sure, use the mentioned plugin ;) – Christian Brabandt Mar 26 '18 at 7:04
  • 2
    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 '18 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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