3
#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

alhelal
  • 659
  • 4
  • 10
  • 17

1 Answers1

12

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).

Elliptical view
  • 305
  • 2
  • 8
Jakube
  • 435
  • 1
  • 4
  • 10