Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make comment around a line of description using vim?

Tags:

c

comments

vim

I thought it would be handy to create a mapping that would turn the current line or visually selected line into a comment banner.

Something like this:

This is the description of the usage of the class

would become, after pressing a shortcut:

/*----------------------------------------------------*/
/* This is the description of the usage of the class  */
/*----------------------------------------------------*/
like image 715
mko Avatar asked Jul 20 '12 05:07

mko


People also ask

How do I comment a specific line in Vim?

To comment out a single line in Vim, enter Visual Mode by pressing Ctrl + V . Next, navigate to the line you wish to comment out and press the C key. Depending on your Vim configuration, this should comment out the selected line. That's all there is to it – easy, isn't it?

How do I add a comment in Vim?

Hit Ctrl + q in GVIM or Ctrl + v in VIM, then go down to select first character on the lines to comment out. Then press c , and add the comment character.

How do you comment on a set of lines?

To comment out multiple code lines right-click and select Source > Add Block Comment. ( CTRL+SHIFT+/ ) To uncomment multiple code lines right-click and select Source > Remove Block Comment. ( CTRL+SHIFT+\ )


1 Answers

I have this line in my ~/.vimrc that does exactly what you want:

nnoremap <leader>g I/* <Esc>A */<Esc>yyp0llv$r-$hc$*/<Esc>yykPjj

Place your cursor on a line, press <leader>g, done.

The default <leader> is \, so that would be \g.

You can, obviously, use any shortcut you want.

like image 138
romainl Avatar answered Oct 17 '22 21:10

romainl