Question
Auto-Format XML and HTML in Notepad++: Indentation and Pretty Printing
Question
Is there a way to automatically re-indent or format a selected block of XML or HTML in Notepad++, similar to Eclipse’s Ctrl+Shift+F auto-format command?
I specifically need a solution that works inside Notepad++ so I can avoid interrupting my workflow. I work with XML, HTML, and CSS, and NppAutoIndent is not suitable for these document types.
Short Answer
XML and HTML formatting is usually called pretty printing: a formatter reads the document structure and rewrites its whitespace and indentation consistently. In Notepad++, this is commonly done with a formatter plugin installed through Plugins Admin, then optionally assigned to a custom keyboard shortcut.
Concept
Indenting XML and HTML correctly is more than inserting spaces at the start of lines. These languages are built from nested elements, so a formatter must understand opening tags, closing tags, attributes, comments, and text content.
For example, this XML is valid but difficult to read:
<order><customer><name>Ada</name></customer><items><item sku="A1">Keyboard</item></items></order>
A pretty printer uses the element nesting to produce readable output:
<order>
<customer>
<name>Ada</name>
</customer>
<items>
<item sku="A1">Keyboard</item>
</items>
</order>
This matters because consistent formatting makes code review, debugging, editing, and finding mismatched tags much easier. It also reduces the chance of changing the wrong nested element.
In Notepad++, formatting support is generally provided by plugins rather than one universal formatter. For XML, an XML-aware plugin such as XML Tools can pretty-print a well-formed XML document. HTML may require an HTML-capable formatter or tidy plugin, depending on the plugins available in your Notepad++ installation. CSS typically needs its own CSS-aware formatter because CSS uses braces rather than nested tags.
Mental Model
Think of XML and HTML as an outline of nested folders.
- An opening tag, such as
<section>, means “enter a folder.” - A closing tag, such as
</section>, means “leave that folder.” - Each folder level moves the text one indentation level to the right.
A formatter walks through that folder structure and places each line at the appropriate depth. It cannot reliably do this by counting characters alone; it needs to recognize the document’s syntax.
Syntax and Examples
A formatter does not add new meaning to a document. It normally changes only presentation: line breaks, indentation, and sometimes spacing around attributes.
XML before and after
<catalog><book id="b1"><title>Clean Code</title><author>Robert C. Martin</author></book></catalog>
<catalog>
<book id="b1">
<title>Clean Code</title>
<author>Robert C. Martin</author>
</book>
</catalog>
HTML before and after
<main><h1>Products</h1><ul><li>Keyboard</li><li>Mouse</li></ul></main>
<main>
<h1>Products</h1>
<ul>
<li>Keyboard</li>
<li>Mouse</li>
</ul>
</main>
Typical Notepad++ workflow
- Open Plugins Admin from the Plugins menu.
- Install an XML formatter plugin, such as XML Tools, for XML documents.
- Reopen Notepad++ if the plugin asks you to restart.
- Open the plugin’s menu and choose its Pretty Print or formatting command.
- Assign a shortcut through Settings > Shortcut Mapper if the command is listed there.
Exact plugin names, commands, and default shortcuts can vary by Notepad++ version and installed plugins. Check the command shown in your own Plugins menu rather than relying on a shortcut from an older tutorial.
Step by Step Execution
Consider this XML:
<profile><contact><email>ada@example.com</email></contact></profile>
A conceptual formatter processes it like this:
- It reads
<profile>and opens a new nesting level. - It reads
<contact>insideprofile, so it increases indentation again. - It reads
<email>, its text value, and</email>. - It sees
</contact>, so it returns to the previous indentation level. - It sees
</profile>, so it returns to the outermost level.
The result is:
<profile>
<contact>
<email>ada@example.com</email>
</contact>
</profile>
If the closing tags do not match the opening tags, an XML formatter may fail or report an error. That is useful: it can reveal structural problems that manual indentation might hide.
Real World Use Cases
- API responses: Pretty-print XML returned by a legacy SOAP service before inspecting values and nested error details.
- Configuration files: Format XML application settings so environment-specific changes are easy to review.
- Web templates: Re-indent HTML after pasting a compressed snippet into a page template.
- Data exports: Make an XML export from a database readable before checking records manually.
- Content management: Clean up edited HTML fragments before storing or reviewing them.
- Style sheets: Use a CSS-specific formatter to make selectors and declarations consistent with a team style.
Real Codebase Usage
Developers usually treat auto-formatting as a repeatable quality step rather than a one-time cleanup.
Common patterns
- Format before committing: Run the formatter before creating a Git commit so whitespace changes are predictable.
- Use project conventions: Teams agree on indentation width, often two or four spaces, and whether tabs are allowed.
- Validate before formatting XML: XML must be well-formed for XML-aware tools to safely understand its tree structure.
- Review formatter changes separately: Avoid mixing a whole-file reformat with an unrelated functional change when possible. Large whitespace-only diffs make reviews harder.
- Use language-specific tools: XML, HTML, and CSS have different grammars. A generic indentation tool is less reliable than a parser-aware formatter.
- Keep source backups or use version control: Some HTML formatters can normalize optional syntax or rewrite markup. Check the diff after formatting important files.
For a fast Notepad++ workflow, install the relevant plugin once and map its formatting command to a shortcut that does not conflict with an existing command.
Common Mistakes
Expecting plain indentation to fix broken XML
This document is structurally invalid because the tags do not match:
<user>
<name>Ada</user>
</name>
Indentation cannot make it valid. Correct the structure first:
<user>
<name>Ada</name>
</user>
Using an XML formatter on ordinary HTML
XML requires every element to be closed and properly nested. Traditional HTML can contain optional closing tags and other constructs that are valid HTML but not valid XML. If XML pretty printing rejects an HTML file, use an HTML-aware formatter instead.
Formatting only part of a document without checking context
A selected fragment may depend on opening tags that appear before the selection or closing tags after it. If a plugin does not support formatting selections safely, format the complete document or work on a copied fragment.
Assuming formatting is always semantics-free
Whitespace is usually cosmetic in element-based XML and HTML, but it can matter in some content. For example, spaces in preformatted HTML content may be intentional:
<pre>keep these spaces</pre>
Review changes around <pre>, code examples, and XML elements whose text is whitespace-sensitive.
Using one formatter for XML, HTML, and CSS
These formats look related but are parsed differently. Install and use a formatter appropriate to the file type rather than expecting an XML indentation plugin to format CSS.
Trusting an old default shortcut
Plugin commands and shortcuts can differ across versions. Use Shortcut Mapper to inspect, change, and document the shortcut you actually use.
Comparisons
| Approach | Understands document structure? | Best for | Limitation |
|---|---|---|---|
| Manual indentation | No | Tiny edits | Slow and easy to get wrong |
| Generic auto-indent | Limited | Source code while typing | May not correctly format nested XML/HTML |
| XML pretty printer | Yes, for well-formed XML | XML documents and XML-based formats | May reject non-XHTML HTML |
| HTML formatter/tidy tool | Yes, for HTML rules | HTML pages and fragments | May normalize or alter markup formatting |
| CSS formatter | Yes, for CSS braces and declarations | .css files and style blocks | Does not format XML/HTML nesting |
A parser-aware formatter is usually the best choice because it uses the language’s structure instead of guessing based on the current indentation.
Cheat Sheet
- Pretty print means rewriting a document with consistent indentation and line breaks.
- For XML, use an XML-aware Notepad++ plugin such as XML Tools.
- Install plugins through Plugins > Plugins Admin.
- Look for a command named Pretty Print, Format, or similar in the plugin menu.
- Use Settings > Shortcut Mapper to assign a convenient keybinding when supported.
- XML must be well-formed: matching tags, one root element, quoted attribute values, and correct nesting.
- Use an HTML-aware formatter for HTML that is not valid XHTML/XML.
- Use a separate CSS formatter for CSS.
- Format before a commit, then inspect the diff for unintended changes.
- Keep formatting-only changes separate from behavior changes when practical.
FAQ
Can Notepad++ auto-format XML?
Yes. Install an XML-aware formatter plugin through Plugins Admin, then use its Pretty Print or formatting command on a well-formed XML document.
What is the Notepad++ shortcut for XML pretty printing?
It depends on the installed plugin and Notepad++ version. Check Settings > Shortcut Mapper and assign your own shortcut if the plugin command is available there.
Why does an XML formatter fail on my HTML file?
Your HTML may be valid HTML but not valid XML. For example, it may use optional closing tags. Use an HTML-aware formatter or convert the markup to valid XHTML when appropriate.
Can I format only selected XML in Notepad++?
Some plugins support selected text, while others require a complete valid document. If formatting a selection fails, format the whole file or copy the fragment into a temporary complete XML document.
Does pretty printing change XML data?
It should primarily change whitespace and line breaks, but whitespace can be meaningful in some XML content. Use version control or a backup and inspect the resulting diff.
Can the same plugin format XML, HTML, and CSS?
Usually not reliably. XML, HTML, and CSS have different syntax rules, so language-specific formatters are safer.
Is NppAutoIndent the same as a formatter?
No. Auto-indent generally helps while typing based on surrounding lines. A formatter or pretty printer analyzes existing document structure and re-indents the document consistently.
Mini Project
Description
Create a small, readable XML product catalog and practice the same formatting workflow used for configuration files and API data. The project demonstrates why nested structure should be formatted consistently before people edit or review it.
Goal
Turn a compressed XML catalog into well-formed, consistently indented XML and verify that its structure is easy to inspect.
Requirements
Start with the provided single-line XML catalog.
Format it with an XML-aware Notepad++ pretty-print command.
Use two spaces for each nesting level if your formatter supports indentation settings.
Confirm that every opening tag has a matching closing tag.
Save the formatted document as catalog.xml.
Keep learning
Related questions
Add a Favicon to a Static HTML Page
Learn how to add a favicon to static HTML pages, use correct paths and icon formats, and troubleshoot browser caching and local-file issues.
Allow Only Numeric Input (0-9) in HTML Input Using jQuery
Learn how to allow only digits 0-9 in an HTML input using jQuery, with examples, validation tips, common mistakes, and best practices.
Apply CSS !important with jQuery .css()
Learn why jQuery .css() cannot set !important and how to use CSSStyleDeclaration.setProperty() to apply inline important styles safely.