755

Is there a way to re-indent a block of code? I'm looking for something similar to Ctrl+Shift+F in Eclipse (Auto-Format/Indent).

To be clear,

  • I already know how to format XML outside of Notepad++ (Eclipse works fine, as mentioned) so I don't need a bunch of links to other XML-formatting tools.
  • I'm specifically working with XML and HTML.
  • Ideally, there's a keybinding as convenient as the one in Eclipse, so I don't have to break my workflow.

I already know about NppAutoIndent - it won't work, as I'm working with XML, HTML and CSS.

1

9 Answers 9

1032

Since I upgraded to 6.3.2, I use XML Tools.

  • install XML Tools via the Plugin Admin (Plugins → Plugins Admin... Then search for "XML Tools", check its box and click the "Install" button).
  • use the shortcut Ctrl+Alt+Shift+B (or menu → Plugins → XML Tools → Pretty Print)

enter image description here

enter image description here

In older versions: menu → TextFX → HTML Tidy → Tidy: Reindent XML.

23
  • 11
    I installed the 5.9.8 version and added TextFX as a plugin. Plugins -> Plugin Manager -> Show Plugin Manager. Select TextFX and install. After a restart of npp, the menu 'TextFS' should be visible.
    – remipod
    Commented Mar 5, 2012 at 8:59
  • 20
    Beware of the missing libtidy.dll error in recent versions when installing the HTMLTidy plugin. Commented Aug 17, 2012 at 4:36
  • 24
    XML Tools will format XHTML, but not HTML. Try formatting this: <form><input><input><input><input><input></form>. Your code needs to be in this format to work, which will be a deal breaker for many developers: <form><input /><input /><input /><input /><input /></form>
    – thdoan
    Commented Jan 19, 2016 at 6:52
  • 1
    Find the Format option in the Menu: Extensions->XML Tools -> Pretty print (with line breaks)
    – fl0w
    Commented Jul 4, 2017 at 7:44
  • 3
    Be very careful with this it will turn <div></div> into <div/> if the div's are empty. This could be a breaking change for web browsers rendering css styles. Commented Aug 3, 2017 at 15:08
60

Install Tidy2 plugin. I have Notepad++ v6.2.2, and Tidy2 works fine so far.

10
  • 6
    I second this, Tidy2 got the job done for HTML markup and does a very good job at it.
    – DWils
    Commented Dec 9, 2013 at 18:47
  • 3
    I third this. XML Tools didn't work for me and couldn't made it work with TextFX Commented Jan 14, 2014 at 19:36
  • 5
    Thanks.. 'XML Tools' didn't work on certain files and this is what worked for me.
    – Subhashi
    Commented Feb 15, 2014 at 0:20
  • 5
    Beware: Certain HTML character entities get replaced with their unicode character when you tidy. This is regardless of the Tidy2 configuration or document encoding. For example, "&#8217;" before tidying becomes "’" ("smart" single quote) after tidying. This made Tidy2 unusable for me. The project has not been updated since 2012.
    – trebormf
    Commented Apr 6, 2015 at 14:45
  • 2
    no option for using tabs instead of spaces for indentation kills this for me.
    – cori
    Commented Jun 6, 2016 at 19:15
37

For those who don't know, npp has a lot of support from plugins and other projects. You can download those plugins from SourceForge.

enter image description here

You need XML Tools to format your text in n++

After you have downloaded XML Tools ..

Exit Notepad++

Go To C:\Program File\Notepad++ .... Your N++ installed folder.

  1. Place below files from xml tools which you downloaded in the npp root folder by copy replace

enter image description here

  1. Go To ..\Plugins subfolder and place below downloaded file

enter image description here

Restart and enjoy!!!

Ctrl + Alt + Shft + B to format.

1
  • 4
    I don't seem to have a plugin manager on mine - I have notepad++ 7.3.3.
    – Owl
    Commented May 11, 2017 at 10:27
15

It's been the third time that I install Windows and npp and after some time I realize the tidy function no longer work. So I google for a solution, come to this thread, then with the help of few more so threads I finally fix it. I'll put a summary of all my actions once and for all.

  1. Install TextFX plugin: Plugins -> Plugin Manager -> Show Plugin Manager. Select TextFX Characters and install. After a restart of npp, the menu 'TextFX' should be visible. (credits: @remipod).

  2. Install libtidy.dll by pasting the Config folder from an old npp package: Follow instructions in this answer.

  3. After having a Config folder in your latest npp installation destination (typically C:\Program Files (x86)\Notepad++\plugins), npp needs write access to that folder. Right click Config folder -> Properties -> Security tab -> select Users, click Edit -> check Full control to allow read/write access. Note that you need administrator privileges to do that.

  4. Restart npp and verify TextFX -> TextFX HTML Tidy -> Tidy: Reindent XML works.

2
  • If you are using the "allowAppDataPlugins" setting (allowing per-user plugins), you can do this by putting the tidy folder from step 2 in your user plugins directory and skipping step 3.
    – palswim
    Commented Apr 5, 2013 at 19:31
  • @palswim: Nice, thanks! Your solution is cleaner when one user uses the npp. Commented Apr 5, 2013 at 20:11
11

Not exactly a solution but a workaround.

Notepad ++ doesn't provide any such feature by default. But you can use some online tools to autoformat text like https://www.freeformatter.com/xml-formatter.html.

1
  • 3
    Although this answer is a workaround rather than a solution to the OPs question, it's the only thing that worked for me. Both XML Tools and Tidy2 have issues. Commented Sep 3, 2018 at 16:37
9

Just install the latest notepad++ and install indent By fold. On the menu bar select Plugins -> Plugins Admin and selct indent By fold and the install. Works finest

3
  • 1
    this should be the accepter answer now, because i can't get xml tool or tidy2 to work. Anyway, can we assign shortcut for this usage? Commented Apr 19, 2020 at 16:23
  • Indent By Fold worked very well for me. I don't appear to get any spaces on empty lines (is that what you meant by 'empty rows' @Henke?). I don't have the AutoSave plugin though. NP++ v8.1.5 and Indent By Fold v0.7.3.
    – wkille
    Commented Oct 7, 2021 at 10:59
  • First of all, I am now on version 8.1.4 (32-bit), and Auto Save works flawlessly. (I have therefore deleted one of the comments in which I complained about this.) Secondly, here is an example screenshot demonstrating some of the problems with Indent By Fold. As a consequence, I have stopped using it and have instead switched to prettyhtml@starptech which is run from the command line, and therefore does not depend on notepad++ or any other editor. Commented Oct 8, 2021 at 14:24
8

I had to update the proxy settings under Plugins -> Plugin Manager -> Show Plugin Manager -> Settings to see any PlugIns in the "Available" list.

After that, installing "XML Tools" was easy and did the requested job as described above.

1
  • 1
    Using "XML Tools" the indent was not as I required for HTML+Javascript code. Found "UniversalIndentGUI" plugin does the job nicely by setting language to HTB(HTML, XML, XSL) and then run 'Indent the text' from plugins menu.
    – GuruM
    Commented Feb 17, 2014 at 13:41
6

To directly answer the OP, take a look at this guy's site: Thomas Hunter Notepad++ Tidy for XML. Simple steps to follow and you get very nice formatting of your XML right inside NPP. So far the only anomaly I've found is with nested self closing elements EG:

<OuterTag>Text for outer element<SelfClosingTag/></OuterTag>

Will be tidied up to:

<OuterTag>Text for outer element
<SelfClosingTag/></OuterTag>

There may be a way to fix this, but for the time being, it's managed to reduce the number of lines in my document by 300k and this particular anomaly can be worked around.

1

I'm using Notepad 7.6 with "Plugin Admin" and I could not find XML Tools.
I had to install it manually like @some-java-guy did in his answer except that my plugins folder was located here: C:\Users\<my username>\AppData\Local\Notepad++\plugins
In that directory I created a new directory (named XmlTools) and copied XMLTools.dll there. (And I copied all dependencies to the Notepad++ directory in Program files.)

1
  • With Notepad++ v7.8.4 , "XML Tools" is available within "Plugin Admin". Perhaps there used to be an incompatibility or something. Still seems like it's actively developed too, considering the latest commit was 2 days ago on github.com/morbac/xmltools Commented Feb 18, 2020 at 11:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.