Adding Code Formatting Tools in Notepad++

Published by Hameed on

I am a big fan of beautiful code, not only in terms of quality of programming but also formatting the code nicely so that it is easy on eyes.

If you are using a full-blown IDE, they come with their own formatting facility, but if you are using a multipurpose editor such as Notepad++, then you may have to use a plugin to format your code. There are many 3rd party programs that can be used with Notepad++ that will help you format your source code without going through a lot of effort.

I use different tools for different languages. I use PerlTidy for formatting my Perl code, and AStyle for certain other languages. Here are the steps taken to add AStyle to Notepad++. Please check the AStyle website for further information on AStyle and its options.

First you need to make sure you have NppExec plugin added to your Notepad++ by going to Plugin menu.

NppExec Menu

NppExec Menu

Download AStyle. The zip archive includes all the source and anย  executable. You can simple copy the executable to a suitable location (For this demo, I am copying it to H:\tools\AStyle.exe. Or you can recompile from the source.

Go to NppExec under Plugin menu and click on Execute or hit F6. You will get the Execute dialogue box.

Execute Dialogue Box

Execute Dialogue Box

Select ” from the dropdown box if it is not already selected.

Type in the following commands in the window:

npp_save
h:\tools\AStyle --style=allman "$(FULL_CURRENT_PATH)"
npp_open $(FULL_CURRENT_PATH)

The first line saves the file. If you do not save first, then it might format the last saved version of your file (Thanks arduinoparty for pointing out). The 2nd command formats the code and saves the file. The view doesn’t get refreshed, so we need to reload the file from disk, and npp_open does that in the above.ย Note that I am using AStyle with –style=allman flag which uses broken brackets.

Hit the save button and it will ask you for a name, just type an appropriate name and hit OK to run.

Execute AStyle

Execute AStyle

You could stop right there and every time you need to run the command, just go to Plugins -> NppExec -> Execute… But let’s go one step further.

Under Plugins, go to NppExec and click on Advanced Options.

NppExec Advanced Options

NppExec Advanced Options

Select “Run AStyle” from the Associated Script drop down menu and then hit the “Add/Modify” button. Make sure the “Place to the Macros Submenu” is ticked.

This will add a new option in Macro Menu.

 

Notepad++ Macro Menu

That is it. Hope you have found this short tutorial useful.


Hameed

Yet another software developer with interest in OO, Open Source, Social Media, politics, Islam and ...

15 Comments

Mike · 6 July 2012 at 4:07 am

Thanks man!

    AbuNoureen · 6 July 2012 at 8:48 am

    You are welcome. I am glad you found this helpful.

Bernard Gisin · 30 March 2013 at 7:52 am

I searched during hours, to find this very usefull information.
I can now use NotePas++ to compile FreePascal programms, using quick shortcuts.
I can also save and execute the compiled programm the same way.
Using NppExec, batch files and the information above.
Maybe I’ll explain more another day.

Marcus Lorenzana · 14 April 2013 at 11:14 am

Thank you so much!

Nikolay · 11 June 2013 at 12:06 am

Awesome! Thanks ๐Ÿ™‚

Edgar · 10 September 2013 at 12:26 pm

NPP_EXEC: “Run Astyle”
C:\Users\Edgar\Desktop\Carpetas\Programacion\AStyle –style=allman “C:\Users\Edgar\Desktop\Sample.java”
CreateProcess() failed with error code 5:
Acceso denegado.

How can i solve this? The location of astyle’s folder is the same on the second line
Carpetas=Folders
Programacion=Programming

    Hameed · 4 October 2013 at 11:08 am

    Hi, Try running AStyle manually from command line and see what happens. Access denied error is most probably related to your file permissions.

Rudy Khoiruddin · 21 November 2013 at 11:41 pm

Thanks.. very good

Now.. i can beautify my code ๐Ÿ™‚

Maddy · 1 March 2014 at 9:19 am

Thanks Buddy

arduinoparty · 16 September 2014 at 3:50 am

Great help. Thanks.
I have found that running the macro changes in the text files are lost if not saved before.
Using NPP_SAVE in the first line of the macro should help I guess. I found an example in:
http://sixdots.de/krlsourcecodeformatter/de/

    Hameed · 6 November 2014 at 11:45 am

    Good catch… I will add this when I get a chance.

      greimel26 · 11 May 2017 at 7:54 pm

      I am glad to find npp_save in your example above.
      I lost my settings and reinstalled everything again. Now I have the nasty effect that every time I call the macro a console window opens and remains open.
      Any hint to that?
      Thanks.

Bryan · 19 October 2017 at 1:22 am

How do you run PerlTidy?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: