Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert comments automatically in Vim

My SAS code requires this style of comment:

/*
* This is the comment
*/

I've been able to type this command (From the Vim Comment Howto):

:set comments=sl:/*,mb:*,elx:*/

The problem is that once I type this set command I don't know how to actually get those comments to add to the code. The instructions say to type /\*<enter> but in insert mode this just acts normally, and in command mode this does a find on *.

How do I get this to work, and are there better ways than this to automatically insert comment marks?

like image 455
Dan Avatar asked Jun 04 '09 16:06

Dan


2 Answers

By default, Vim doesn't automatically insert the newlines or end markers for you. Instead, it makes it easy to insert those as you type, as long as 'formatoptions' contains r:

:set formatoptions+=r

After this, start typing your comment as normal: "/*<Enter>" (in insert mode). After you hit the Enter key, the comment leader (an asterisk and a space) should appear automatically on the next line, ready for you to start typing. When your comment is complete, end it with "<Enter>/"; the <Enter> moves to the next line, and the slash becomes the second character of the end marker. Yes, it will remove the space for you, but only right after you hit enter.

To make editing this type of comment easier, you wish to add the c and/or o characters to formatoptions, as well. The former allows comments to auto-wrap, and the latter inserts the comment leader when you create a new line inside the comment using normal-mode commands.

like image 95
eswald Avatar answered Oct 13 '22 12:10

eswald


Which language?

In C Vim autoloads this setting for comments:

" Set 'comments' to format dashed lists in comments.
setlocal comments=sO:*\ -,mO:*\ \ ,exO:*/,s1:/*,mb:*,ex:*/,://

Which works as you'd expect. Maybe you need to add that to the ftplugin for the language/extension you're using?

like image 43
John Weldon Avatar answered Oct 13 '22 12:10

John Weldon