Click here to Skip to main content
14,212,683 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

QuestionHow do I install/use it? Pin
17-Feb-01 11:14
suss17-Feb-01 11:14 
AnswerRe: How do I install/use it? Pin
Mark Findlay18-Feb-01 7:29
memberMark Findlay18-Feb-01 7:29 
Generalprocessing large files Pin
Aaron Sulwer14-Feb-01 7:02
memberAaron Sulwer14-Feb-01 7:02 
Generalprocessing large files Pin
Aaron Sulwer14-Feb-01 7:01
memberAaron Sulwer14-Feb-01 7:01 
GeneralRe: processing large files Pin
Alvaro Mendez14-Feb-01 8:22
memberAlvaro Mendez14-Feb-01 8:22 
GeneralRe: processing large files Pin
Aaron Sulwer14-Feb-01 8:34
memberAaron Sulwer14-Feb-01 8:34 
GeneralRe: processing large files Pin
Alvaro Mendez14-Feb-01 9:20
memberAlvaro Mendez14-Feb-01 9:20 
GeneralRe: processing large files Pin
Aaron Sulwer14-Feb-01 9:28
memberAaron Sulwer14-Feb-01 9:28 
actually it happens with every large file that i have. I have used this on all sorts of code and it only craps on large files. i will send you one to test.
GeneralRe: processing large files Pin
31-Jul-01 3:28
suss31-Jul-01 3:28 
GeneralVC 6 bug with this macro Pin
3-Nov-00 11:15
suss3-Nov-00 11:15 
GeneralRe: VC 6 bug with this macro Pin
9-Jan-01 10:57
suss9-Jan-01 10:57 
GeneralRe: VC 6 bug with this macro Pin
10-Jan-01 3:58
suss10-Jan-01 3:58 
GeneralRe: VC 6 bug with this macro Pin
Joshua Jensen10-Jan-01 4:48
memberJoshua Jensen10-Jan-01 4:48 
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 
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 

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.

Layout: fixed | fluid

Article Copyright 1999 by Alvaro Mendez
Everything else Copyright © CodeProject, 1999-2019

Server Web01
Version 2.8.190619.2