Reformat C/C++ Comments

Click to download source

This is a macro for use with the Visual C++ editor to deal with the common problem of modifying a multi-line C/C++ comment and then having to manually change the length of every line to make the text word-wrap nicely.

This macro will automatically word-wrap the comment for you.  It will even increase or decrease the number of lines in the comment as necessary, and stick in appropriate leading characters (e.g. "*", "//", etc.) on new comment lines.

Installation

Copy the fmt.dsm file to your computer.  Tell Visual C++ to load it via Tools > Customize > Add-ins and Macro Files > Browse.  Assign it to a key (Ctrl+Q is a good choice) via Tools > Customize > Keyboard, Category "Macros," command "FormatComment."

Usage

First, block-select the C/C++ comment.  This is done by holding down Alt while selecting the text of the comment.

The left column of your selection should be the first column of the actual text of the comment, not including any leading comment characters such as "//".  The right column of your selection should be the column at which you want the comment to wrap.  This column may be either to the left or to the right of the current ends of the lines.  For example:

// This function does something very important.  However, the
// actual task performed is so top-secret that we really can't
// get into it here.  If you would like to know more, please
// send in your security clearance proof.                 

Then, just run the macro called "FormatComment."

Notes

FormatComment is intelligent about common commenting styles.  In general, it repeats whatever comment-leading characters were present on the second line of the original comment.  Here are some examples of a two-line comment reformatted as a three-line one.  Notice that FormatComment does the best it can to preserve the programmer's own commenting style.

Before reformatting After reformatting
// First line of comment.
// Second line of comment.
// First line of
// comment.  Second
// line of comment
/* First line of comment.
 * Second line of comment.
 */
/* First line of
 * comment.  Second
 * line of comment
 */
/* First line of comment.
** Second line of comment.
*/
/* First line of
** comment.  Second
** line of comment
*/
/* First line of comment.
   Second line of comment. */
/* First line of
   comment.  Second
   line of comment */

Back to the "Code" page