Click here to Skip to main content
14,209,828 members
Click here to Skip to main content
Posted 16 Nov 1999


80 bookmarked

Reformat source code

Rate this:
4.97 (34 votes)
Please Sign up or sign in to vote.
4.97 (34 votes)
26 Sep 2005        
A great macro for reformatting C++ source code.


MakeCodeNicer began as a Visual C++ 6.0 macro used for reformatting C/C++ source files on the fly, right inside the editor. It was used by developers who needed to quickly reformat source files (usually written by other people) for their own use.

Visual Studio 2002 and 2003 introduced a new macro engine, which unfortunately broke MakeCodeNicer. Anders Dalvander was kind enough to convert the code so that it would compile, but for some reason, it didn't quite work as expected.

Well, I've finally taken some time to finish Anders' port so that this macro can once again be used inside Microsoft's development environment. I'm going to keep the VC6 version available for download, but since I don't have VC6 installed, I can't support that version any more.


To install the VS.NET version:

  1. For 2003, extract the zip file to your "My Documents\Visual Studio Projects\VSMacros71\". (For 2005 and later, please see the Notes section below.) Be sure to "Use folder names" when extracting.
  2. Inside Visual Studio, go to Tools/Macros/Load Macro Project.
  3. Select the ReformatCode.vsmacros file (inside the ReformatCode folder).
  4. In the Macro Explorer, expand the ReformatCode module and then the MakeCodeNicer macro.
  5. You'll see the SelectionOnly and WholeFile methods.
  6. Open a C, C++, C#, Java, or JavaScript source file to be reformatted. Make sure it compiles successfully!
  7. If you want the whole file to be reformatted, double click on the WholeFile method and sit back to let it do its thing. While it's running, you'll see an animated cassette in the system tray, which can be right-clicked to interrupt the macro.
  8. If you only want to reformat a portion of the file, select the portion, and double click on the SelectionOnly method.

What it does

Once the macro is done, you'll see that it does several things to your source code:

  • Adds extra spaces in a few places and removes them from other places:
    • Appends a space to if, for, foreach, while, switch, catch, return, throw, lock, and using statements.
    • Removes any spaces before or after method parenthesis.
    • Adds a space before and after operators (such as =, ==, etc.).
    • Appends a space to commas and semicolons (inside for loops).
    • Inserts a space between the // and the first character of inline comments.
  • Breaks single-line if, for, foreach, and while statements into multiple lines (for easier debugging).
  • Breaks up case statements into their own lines and indents them if necessary.
  • Isolates braces ({ }) into lines by themselves.
  • Removes trailing white space from all lines in the file.
  • Removes extraneous blank lines.
  • Accounts for code within quoted strings or characters.
  • Ignores code after inline comments (//).

If you've worked with MFC in the past and followed its conventions, you'll no doubt recognize this macro's formatting. It's clean and very easy to read, in my opinion.


I ported and tested this macro using C# source files, but it should work with any of the C-derived languages. If you find any problems, please let me know.

I'm also happy to report that Visual Studio .NET 2005 finally makes this macro obsolete. If you need to reformat source code, simply press Ctrl+E, D and the file will be reformatted according to your settings (inside Tools/Options - Text Editor/Language/Formatting).

Enjoy it!


5 May 2000

  • Fixed cases when code gets incorrectly unindented if one or more curly braces appear inside quoted strings. (Thanks to Bob Eastman for reporting it.)
  • Fixed cases when "operator=(...)" turns into "operator =(...)".
  • Added more up-front validations to ensure the macro can be run properly.
  • Made the macro execute on a temporary file which is then copied onto the target source code. This allows undoing of all of the macro's changes in one step. (Thanks to Nicolas Fleury for suggesting it.)

16 Feb 2001

  • Added a check for Visual C++'s "out of memory" bug which occurs when the macro is invoked on multiple very-large source files. (Thanks to Aaron Sulwer for reporting and helping me test this problem.)
  • Added code to remove spaces before and after -> operators. (Thanks to Dominic Holmes for suggesting it.)
  • Fixed erroneous space removals before certain opening parenthesis and after inlined functions. (Thanks to Joe Woodbury for reporting these.)

26 Apr 2001

  • Changed the logic that fixes less than and greater than operators to only do so when they're inside "for", "while", and "if" statements. Since these operators have so many uses in C++, it's very difficult to accurately fix them across the board. Thanks to Robin Summerhill and Thomas Freudenberg for bringing these to my attention.
  • Replaced Int() with CInt() in two locations. This should prevent a type mismatch error reported by Henning Flessner.

9 July 2002

  • Stopped multi-line preprocessor macros from being reformatted. (Thanks to Niels Harremoës for reporting it.)
  • Fixed improper splitting of lines with multiple semicolons. (Thanks to Jon for reporting it.)
  • Adjusted the FILE DESCRIPTION comment to allow the macro's description to be displayed on the "Add-ins and Macro Files" tab of the Customize box. (Thanks to Michael Martin for reporting it.)

15 Jul 2002

  • Fixed possible Type Mismatch error. (Thanks to henning flessner for reporting it and providing the solution.)
  • Stopped code containing "','," from turning into "', ',". (Thanks to Dirk for reporting it.)

16 Aug 2005

  • Ported to Visual Studio .NET 2003. Thanks to Anders Dalvander for giving me a great jump start in the effort.
  • I converted all variables to be type specific to improve performance. I also created a local variable in most methods to cache the ActiveDocument.Selection.
  • I fixed several minor issues, thanks to the new ability to debug (woo hoo).

15 Sep 2005

  • Fixed conversion of < and > operators when used for template notation. (Thanks to Eddie Parker for reporting the problem.)
  • Ported to Visual Studio .NET 2005 (Beta 2). Now all it does is run the "SmartFormat" method (which can easily be invoked with Ctrl+E, D). This macro has finally become obsolete. :-)


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alvaro Mendez
Web Developer
United States United States
I've done extensive work with C++, MFC, COM, and ATL on the Windows side. On the Web side, I've worked with VB, ASP, JavaScript, and COM+. I've also been involved with server-side Java, which includes JSP, Servlets, and EJB, and more recently with ASP.NET/C#.

Comments and Discussions

GeneralRe: VC 6 bug with this macro Pin
Alvaro Mendez11-Jan-01 8:21
memberAlvaro Mendez11-Jan-01 8:21 
GeneralRe: VC 6 bug with this macro Pin
Joshua Jensen11-Jan-01 14:51
memberJoshua Jensen11-Jan-01 14:51 
GeneralRe: VC 6 bug with this macro Pin
Alvaro Mendez11-Jan-01 8:24
memberAlvaro Mendez11-Jan-01 8:24 
GeneralRe: VC 6 bug with this macro << FIX! Pin
Jim A. Johnson16-Feb-01 9:47
memberJim A. Johnson16-Feb-01 9:47 
GeneralRe: VC 6 bug with this macro Pin
19-Feb-01 8:52
suss19-Feb-01 8:52 
GeneralRe: VC 6 bug with this macro Pin
20-Feb-01 2:19
suss20-Feb-01 2:19 
GeneralRe: VC 6 bug with this macro Pin
Kannan Kalyanaraman20-Feb-01 19:37
memberKannan Kalyanaraman20-Feb-01 19:37 
GeneralMinor problem Pin
Richard Bienick18-Oct-00 9:07
sussRichard Bienick18-Oct-00 9:07 
Very nice macro! Just what I was looking for. Too bad we can't turn off screen updates before it runs so that it will work faster. I'm considering translating it to Codewright which may make it faster and offer more control.

I noticed a minor problem in handling switch statements embedded in case statements. It seems that whenever a switch statement is encountered when processing a case statement from an enclosing switch, the indentation of any case or default statement following the embedded switch is off by one tab.

switch (s[0])
case '?':

case 'h':
switch (s[1])
case 'l':

case 'd': // problem - indented one too many

GeneralRe: Minor problem Pin
Alvaro Mendez15-Feb-01 8:47
memberAlvaro Mendez15-Feb-01 8:47 
GeneralRe: Minor problem Pin
15-Feb-01 10:20
suss15-Feb-01 10:20 
General-> Suggestion Pin
Dominic I. Holmes14-Sep-00 3:45
memberDominic I. Holmes14-Sep-00 3:45 
GeneralRe: -> Suggestion Pin
Alvaro Mendez15-Feb-01 10:07
memberAlvaro Mendez15-Feb-01 10:07 
GeneralSome minor problems Pin
Joe Woodbury31-May-00 11:54
professionalJoe Woodbury31-May-00 11:54 
GeneralRe: Some minor problems Pin
Alvaro Mendez15-Feb-01 10:03
memberAlvaro Mendez15-Feb-01 10:03 
QuestionOnly with VC++ 6? Pin
Member 441327-Apr-00 2:39
memberMember 441327-Apr-00 2:39 
AnswerNo, but this problem happens only in VC5. Pin
Alvaro Mendez27-Apr-00 4:11
memberAlvaro Mendez27-Apr-00 4:11 
GeneralCopy-Paste and Undo Pin
Nicolas Fleury20-Apr-00 13:41
memberNicolas Fleury20-Apr-00 13:41 
GeneralRe: Copy-Paste and Undo Pin
Alvaro Mendez20-Apr-00 14:07
memberAlvaro Mendez20-Apr-00 14:07 
QuestionWhat's New? Pin
Oz Solomonovich13-Apr-00 12:24
sussOz Solomonovich13-Apr-00 12:24 
AnswerWhat's New. Pin
Alvaro Mendez13-Apr-00 12:36
memberAlvaro Mendez13-Apr-00 12:36 
GeneralI add this for comment alignment also. Pin
Dave Foringer10-Apr-00 4:23
sussDave Foringer10-Apr-00 4:23 
GeneralCrash Pin
Member 108316-Mar-00 3:44
memberMember 108316-Mar-00 3:44 
GeneralRe: Crash Pin
Alvaro Mendez16-Mar-00 3:58
memberAlvaro Mendez16-Mar-00 3:58 
GeneralBreak lines Pin
pvitali9-Mar-00 1:04
memberpvitali9-Mar-00 1:04 
GeneralRe: Break lines Pin
Alvaro Mendez16-Mar-00 7:34
memberAlvaro Mendez16-Mar-00 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190617.3 | Last Updated 26 Sep 2005
Article Copyright 1999 by Alvaro Mendez
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid