Dan
Dan

Reputation: 10171

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?

Upvotes: 12

Views: 18035

Answers (5)

puk
puk

Reputation: 16802

Also remember to check your comments style (:set comments?) if you are using multiple file types. PHP, for example will sometimes use HTML style comments <!-- ... --> if there is embedded HTML, hence typing /* and then pressing Enter will appear to have no effect.

I have the following in my .vimrc file to make sure PHP comments are used by default

au Bufenter *.php set comments=sl:/*,mb:*,elx:*/

HTML code will still be properly commented, however, spaces within HTML code might use the PHP commenting convention (if you use plugins like tComment) and you won't have multi line HTML comments, which I don't think are possible anyways.

Upvotes: 1

eswald
eswald

Reputation: 8406

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.

Upvotes: 23

webwesen
webwesen

Reputation: 1262

this Vim script might solve your problem - just put it into "vimXY/syntax" folder

Upvotes: 0

Paolo Tedesco
Paolo Tedesco

Reputation: 57302

I have this abbreviation in my .vimrc:

" /// -> insert javadoc comment
iab <buffer> /// /**^M *^M*/^[0A

where ^[0A is ctrl-v + up.
Type /// in insert mode to get a comment like

/**
 * 
 */

Upvotes: 1

John Weldon
John Weldon

Reputation: 40799

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?

Upvotes: 3

Related Questions