How-to
How to format XML on a Mac (pretty-print)
To format XML on a Mac, open Terminal and run xmllint --format file.xml -o file.formatted.xml. xmllint ships with macOS, indents with two spaces by default, and reads XMLLINT_INDENT if you want four spaces or tabs. In our test it also added an XML declaration, collapsed empty elements and indented inside xml:space="preserve", so format a copy, not a file another system checks byte for byte.
1. Three ways to format XML on a Mac
Formatting, also called pretty-printing or beautifying, puts each element on its own line and indents it by depth, so a one-line export becomes readable. It does not repair broken XML: the file has to parse first.
Which way to use depends on how often you do it and how big the file is.
- Terminal: xmllint is already installed on macOS. Best for one file, scripts, or very large files.
- A code editor: VS Code formats XML with Shift-Option-F once an XML extension is installed.
- An XML app: shows the formatted document as a tree beside the source and adds minify, search and validation.
2. How do I format XML from the Mac command line?
Use xmllint --format. It parses the file, then writes it back out with one element per line and two-space indentation. Without an output option it prints to the Terminal window; add -o to write a file.
We ran these commands on macOS 27.2, where /usr/bin/xmllint reports libxml version 20913 (libxml2 2.9.13). The input was a one-line invoice: <invoice id="INV-1042"><customer><name>Acme Corp</name>…</invoice>.
# Print an indented copy
xmllint --format invoice.xml
# Write it to a new file and keep the original
xmllint --format invoice.xml -o invoice.formatted.xml
# Output
<?xml version="1.0"?>
<invoice id="INV-1042">
<customer>
<name>Acme Corp</name>
<email>billing@acme.example</email>
</customer>
<lines>
<line sku="A-100" qty="2">Widget</line>
<line sku="B-200" qty="1">Gadget</line>
</lines>
<note>Paid <b>in full</b> on delivery</note>
</invoice>- Format in place with -o and the same name: xmllint --format invoice.xml -o invoice.xml worked in our test and rewrote the file.
- Never use a shell redirect onto the same file. xmllint --format invoice.xml > invoice.xml left a 0-byte file and the error "Document is empty", because the shell empties the file before xmllint reads it.
- Format XML on the clipboard: pbpaste | xmllint --format - | pbcopy. The dash tells xmllint to read standard input; pbpaste and pbcopy are the macOS clipboard commands.
- Already-indented files are re-indented too: a file indented with eight spaces came out with two.
- Large files are not a problem: a generated 13 MB feed with 200,000 items formatted to a 16 MB file in 0.35 seconds on our test Mac.
3. How do I make xmllint use 4 spaces or tabs?
Set the XMLLINT_INDENT environment variable for that one command. The xmllint manual documents two spaces as the default; whatever string you put in the variable is used once per nesting level.
# Four spaces
XMLLINT_INDENT=' ' xmllint --format invoice.xml -o invoice.formatted.xml
# Tabs (zsh and bash)
XMLLINT_INDENT=$'\t' xmllint --format invoice.xml -o invoice.formatted.xml
# Make four spaces the default in new Terminal windows
echo "export XMLLINT_INDENT=' '" >> ~/.zshrc4. What does xmllint --format change besides indentation?
More than whitespace. xmllint re-serializes the parsed document, so some markup comes out spelled differently. Each line below is a before-and-after we reproduced with the macOS xmllint described above; none of the top results for this search mention them.
Most of these don't change what the data means: any XML parser reads <empty></empty> and <empty/> the same way. They matter when a person or a tool compares the files as text, and the whitespace changes can matter to the receiving system.
# Before
<r><empty></empty><a code='X1'>café</a></r>
# After xmllint --format
<?xml version="1.0"?>
<r>
<empty/>
<a code="X1">café</a>
</r>
# Before (xml:space asks applications to keep white space)
<doc><pre xml:space="preserve"><line>a</line><line>b</line></pre></doc>
# After: indentation added inside the preserved element
<?xml version="1.0"?>
<doc>
<pre xml:space="preserve">
<line>a</line>
<line>b</line>
</pre>
</doc>- An XML declaration is added when the file had none: <?xml version="1.0"?>.
- Empty elements written as a start and end tag become self-closing.
- Single-quoted attribute values are rewritten with double quotes.
- Character references are rewritten: é became é when no encoding was declared, and the literal é when the declaration said UTF-8. Add --encode UTF-8 to choose the output encoding.
- Indentation is added inside elements marked xml:space="preserve", although the XML 1.0 specification says that value signals the intent to keep all white space.
- Mixed content is left alone: <note>Paid <b>in full</b> on delivery</note> stayed on one line, so text around inline elements is not altered.
- Text inside an element keeps its spaces: <name> Acme </name> was unchanged.
5. Why won't my XML format?
Because it isn't well-formed. xmllint prints the parser error with a line number and writes no formatted output, so fix the reported error first and run the command again.
xmllint also has --recover, which prints whatever it could parse. Avoid it for real files: on the broken example below it quietly closed the <b> element where it guessed, and the libxml2 manual describes its behavior as unspecified and discourages its use.
$ xmllint --format bad.xml
bad.xml:1: parser error : Opening and ending tag mismatch: b line 1 and a
<a><b>text</a>
^
bad.xml:1: parser error : Premature end of data in tag a line 16. Format XML in a Mac app instead of Terminal
An XML app is easier when you format XML several times a day, paste it from logs or APIs, or need to read the result rather than only save it. In XML Lens you open, paste or drag in a document and pretty-print it; the structure tree beside the source lets you collapse the parts you don't need.
The same workspace minifies XML for compact payloads, strips comments, and has two presets, Compact for API and Readable for review. Batch processing applies formatting or minification to many selected files and writes the results to an output folder you choose, so the originals are not overwritten. All of it runs on your Mac; XML Lens does not upload documents.
XML Lens is a Mac app on the Mac App Store and free to start. For a quick test without installing anything, the formatter on the XML Lens home page formats pasted XML inside your browser tab.
7. How do I format XML in VS Code on a Mac?
Install an XML extension, then press Shift-Option-F (Format Document). VS Code's documentation lists built-in formatters only for JavaScript, TypeScript, JSON, HTML and CSS, so an XML file needs an extension such as Red Hat's XML extension before Format Document does anything.
To format only part of a file, select it and press Command-K, then Command-F (Format Selection). If you want XML files formatted every time you save, turn on the editor.formatOnSave setting. Other editors such as BBEdit, Sublime Text and IntelliJ have their own XML formatting commands or plugins; check their documentation for the current menu names.
8. When you should not reformat XML
Reformat a copy, and keep the original, whenever another system will read the file. Three cases need extra care.
- Signed XML. Canonical XML, which XML signatures are computed over, normalizes quotes, empty elements and character references but keeps all white space in character content. The indentation that formatting adds therefore changes what was signed, and the signature no longer verifies.
- Whitespace that matters. Content under xml:space="preserve", or text a receiving system reads literally, can change meaning when indentation is added inside it, as the test in step 4 shows.
- Text comparisons and version control. Formatting rewrites every line. To see real changes, format both versions the same way first, or compare the documents by structure instead of by line.
Common questions
Does macOS have a built-in XML formatter?
Yes, in Terminal. xmllint is installed with macOS, and xmllint --format file.xml prints an indented copy. TextEdit opens XML but does not indent it.
How do I format an XML file in place with xmllint?
Use the output option with the same file name: xmllint --format file.xml -o file.xml. Do not redirect with > onto the same file; the shell empties it before xmllint reads it.
How do I set xmllint to indent with 4 spaces?
Run XMLLINT_INDENT=' ' xmllint --format file.xml. The variable holds the string used for each indentation level; the default is two spaces.
Does formatting change my XML data?
It doesn't change element names, attribute values or text inside elements, but it adds whitespace between elements and can rewrite the declaration, quotes, empty elements and character references. Systems that compare bytes, check signatures or keep whitespace can see that as a change.
How do I make XML human readable without installing anything?
Run xmllint --format file.xml in Terminal, or paste the XML into the in-browser formatter on the XML Lens home page, which formats it inside your browser tab without uploading it.
References
xmllint manual (libxml2): --format, XMLLINT_INDENT, --recover ↗W3C XML 1.0 (Fifth Edition): 2.10 White Space Handling ↗W3C Canonical XML 1.0: what canonicalization normalizes ↗VS Code docs: Basic editing, formatting ↗XML extension for VS Code (Red Hat): document formatting ↗Try the workflow in XML Lens
XML Lens is a native Mac app for viewing, validating, querying and comparing XML that keeps your files on your Mac. It is listed as XML Editor Lens: XPath on the Mac App Store.
Download XML Lens for MacExplore the relevant Mac workspaces and their capabilities.
View, read, and format XML on your Mac Batch-process XML files on Mac without overwriting originals Compare XML files side by side on Mac