672

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.

Mwiza
  • 4,494
  • 2
  • 33
  • 30
SBoss
  • 8,255
  • 6
  • 25
  • 44

9 Answers9

898

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.

Gaurav khurana
  • 2,262
  • 1
  • 18
  • 22
remipod
  • 10,241
  • 1
  • 20
  • 21
  • 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 Mar 05 '12 at 08:59
  • 19
    Beware of the [missing libtidy.dll error](http://stackoverflow.com/questions/6985637/notepad-htmltidy-unable-to-find-libtidy-dll) in recent versions when installing the HTMLTidy plugin. – Amos M. Carpenter Aug 17 '12 at 04:36
  • With Notepad++ 6.3.1, TextFX Characters 0.2.6, I select the *Tidy: Reindent XML* command, and it does nothing (and my XML does not have proper indentation). Am I missing a step? – palswim Apr 01 '13 at 21:04
  • 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 Feb 17 '14 at 13:41
  • ...and then I thought "let's Google XML auto indent" and see if I can avoid all this annoying manual work :-D – RubenGeert Sep 10 '14 at 12:51
  • Is it just me? When I open the plug-in manager, nothing is listed. -- Also, the sourceforgelink @SBoss posted is broken. :-/ – BrainSlugs83 Jul 16 '15 at 21:38
  • 15
    XML Tools will format XHTML, but not HTML. Try formatting this: `
    `. Your code needs to be in this format to work, which will be a deal breaker for many developers: `
    `
    – thdoan Jan 19 '16 at 06:52
  • 1
    Find the Format option in the Menu: Extensions->XML Tools -> Pretty print (with line breaks) – fl0w Jul 04 '17 at 07:44
  • Menu: Plugins -> XML Tools -> Pretty print (XML only - with line breaks) – Josh Jul 07 '17 at 21:05
  • 1
    Be very careful with this it will turn
    into
    if the div's are empty. This could be a breaking change for web browsers rendering css styles.
    – Timothy Gonzalez Aug 03 '17 at 15:08
  • @remipod You made so many points with this answer :D Nice one! Thank you for the info. – sunlover3 Dec 08 '17 at 15:43
  • for those finding problem with the xlm, https://sourceforge.net/p/npp-plugins/bugs/318/ – Dasser Basyouni Jan 28 '18 at 04:51
  • 1
    I find this tool has no effect. None the pretty-print options do anything - all my XML is still on one line. – NickG Apr 09 '18 at 11:06
  • @NickG - make sure you are using Pretty print (XML only - with line breaks) option. – myroslav Apr 09 '18 at 17:08
  • I will just add that you need to ensure to add content of `ext_libs` directory (in the plugin archive) into Notepad++ root directory (where notepad++.exe is) – JoDev Jun 22 '18 at 09:40
  • Is this solution works in notepad 7.9 64? Anytime i try to Prety Print i got PluginCommand Exception and no found solution for this. – kwadratens Nov 12 '20 at 11:11
  • I find that this does not close tags for me. I see an option "tag auto-close" but it's not working. Lots of tags with no closing is causing there to be far more indents that are actually needed. Any suggestions on how to get it to auto-fix single-line elements that are not properly closed?
    is another often not closed. I'm trying to read 3rd-party's mess.
    – Daniel Williams Nov 22 '20 at 22:07
  • @timothy-gonzalez In the XML Tools plugin's Options menu, there is an option "Enable auto-closing tags (ex: --> )". You can disable this option to avoid collapsing such tags. – Pi Da Nov 28 '20 at 11:54
  • This is a poor answer for formatting HTML. The fact is, NP++ is terrible at formatting HTML because they don't understand tags that don't need to be closed (i.e. "input"). It's trivial to find online tools that do a great job, how is it that NP++ hasn't figured this out yet? – BrianVPS Apr 14 '21 at 13:54
55

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

Dima
  • 1,707
  • 2
  • 22
  • 29
  • 5
    I second this, Tidy2 got the job done for HTML markup and does a very good job at it. – DWils Dec 09 '13 at 18:47
  • 2
    I third this. XML Tools didn't work for me and couldn't made it work with TextFX – Christian Vielma Jan 14 '14 at 19:36
  • 4
    Thanks.. 'XML Tools' didn't work on certain files and this is what worked for me. – Subhashi Feb 15 '14 at 00:20
  • 4
    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, "’" before tidying becomes "’" ("smart" single quote) after tidying. This made Tidy2 unusable for me. The project has not been updated since 2012. – trebormf Apr 06 '15 at 14:45
  • no option for using tabs instead of spaces for indentation kills this for me. – cori Jun 06 '16 at 19:15
  • I fourth this. I was looking for a code beautifier and found this Q&A and used Tidy2, a great plugin and works beautifully. I've tried a few others but the results weren't what I was looking for and were quite too complicated "settings-wise". – Funk Forty Niner Aug 14 '16 at 20:04
  • Tried to use Tidy2 to auto-indent XML, but all I got were vague error messages. (wait, my XML might have been malformed...) – Baodad Jun 15 '17 at 13:53
  • Note: when configuring Tidy2, be sure to set "output-xml" to no if you're dealing with HTML, otherwise you'll get a document full of unicode characters – Joe Coyle Nov 27 '18 at 13:58
  • Tidy2 deletes empty `` tags and applies only on all document, not on selection, my vote-up was the naive first reaction – Fanky Nov 27 '20 at 15:22
  • Tidy2 still works with the latest 32-bit Notepad++ (7.9.1). However, it completely fails if your HTML contains any tags it doesn't recognize (e.g. ). This prevented me from tidying up a web app's HTML content that used custom tags. Once I removed the custom tags, it worked fine. – Pi Da Nov 28 '20 at 12:05
36

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.

Stacked
  • 5,676
  • 5
  • 52
  • 69
Some Java Guy
  • 4,530
  • 17
  • 61
  • 97
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.

Community
  • 1
  • 1
Vladimir Grigorov
  • 9,951
  • 7
  • 55
  • 65
  • If you are using the ["allowAppDataPlugins" setting](http://notepad-plus-plus.org/download/v5.9.8.html) (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 Apr 05 '13 at 19:31
  • @palswim: Nice, thanks! Your solution is cleaner when one user uses the npp. – Vladimir Grigorov Apr 05 '13 at 20:11
10

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.

bad_coder
  • 5,829
  • 13
  • 26
  • 41
Daksh Agrawal
  • 689
  • 1
  • 10
  • 20
  • 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. – Nigel Alderton Sep 03 '18 at 16:37
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.

Oliver Meyer
  • 363
  • 5
  • 6
  • 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 Feb 17 '14 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.

delliottg
  • 3,534
  • 1
  • 33
  • 45
3

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

Daniel Nyamasyo
  • 2,028
  • 19
  • 22
  • 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? – Irfandi D. Vendy Apr 19 '20 at 16:23
  • 1
    The latest version of Notepad++ (7.8.6) is _very_ disappointing. I tried installing IndentByFold into version 7.5.6, and it broke the wonderful AutoSave plugin by Franco Stellari, [https://sites.google.com/site/fstellari/nppplugins](https://sites.google.com/site/fstellari/nppplugins). Sorry: for me, that's a deal-breaker.(!) Probably nothing wrong per se with IndentByFold by Frank Fesevur. For the time being, I blame the latest version of Notepad++, [https://community.notepad-plus-plus.org/topic/16449](https://community.notepad-plus-plus.org/topic/16449). Pitiable. – Henke May 22 '20 at 17:41
  • I risk sliding off-topic here, but if anyone can report a version of Notepad++ **later** than 7.5.6 that works with the AutoSave 1.60 plugin by Franco Stellari, [https://sites.google.com/site/fstellari/nppplugins](https://sites.google.com/site/fstellari/nppplugins), I would be more than happy to switch to that version. If not - there is no way I will update Notepad++ 7.5.6 to a later version. Version 0.7 of IndentByFold works for me too. But I object to putting (trailing) spaces into empty rows, which IndentByFold does. – Henke May 23 '20 at 16:26
  • My Notepad++ version is 7.5.6. I have now tried 1) XML Tools, 2) HTML Tidy, 3) Tidy2, AND 4) IndentByFold 0.7. XML Tools kind of "works", but makes a deplorable mess of my HTML code, [https://i.imgur.com/oLOB30Z.png](https://i.imgur.com/oLOB30Z.png). Neither HTML Tidy nor Tidy2 work at all. IndentByFold 0.7 is the only solution I can get to work. I don't like that it puts (trailing) spaces on empty rows, but I can live with that. I still don't support this answer (by Daniel Nyamasyo). The reason is that I **strongly** disagree with the advice to use the _latest_ version of Notepad++. – Henke May 24 '20 at 11:50
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.)

Björn
  • 2,436
  • 2
  • 21
  • 33
  • 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 https://github.com/morbac/xmltools – Micah Lindstrom Feb 18 '20 at 11:54