How to Format XML in Visual Studio Code
Learn how to format and beautify XML code in Visual Studio Code, resolve common layout errors, configure extensions, and apply custom formatting rules.
Beautifying and formatting XML in Visual Studio Code requires installing an extension like XML Language Support by Red Hat or Pretty XML, then applying formatting commands such as Ctrl + Shift + K or Control+K L. Because Visual Studio Code does not include native XML formatting out of the box for all schemas, setting up the right extension ensures your documents adopt correct indentation, proper attribute wrapping, and valid syntax checks.
Unformatted XML Blocks Appearing as a Single Cramped Line upon Opening
When you open an XML file in Visual Studio Code and discover that all elements, tags, and text content have collapsed into a single, unreadable horizontal line, the underlying cause is the absence of an active XML formatter extension or an unformatted document state. Without a registered extension handling the .xml file extension, Visual Studio Code treats the file as plain text and offers no automatic beautification.
To resolve this symptom, you must install an extension designed for XML formatting. The XML Language Support extension by Red Hat provides built-in document formatting and syntax validation powered by the LemMinX XML Language Server, which does not require Java since version 0.15.0. Alternatively, the Pretty XML extension formats XML documents just like Visual Studio on Windows, supporting file extensions such as xml, xaml, axml, xsd, xsl, plist, mobileconfig, config, csproj, svg, and resx. Once installed, you can trigger formatting using the command palette or keyboard shortcuts. For Red Hat XML, press Ctrl + Shift + K. For Pretty XML, use Control+K L on Windows and Linux, or Cmd+K L on macOS. If you want formatting to happen automatically every time you save your file, you can enable the setting prettyxml.settings.formatOnSave by switching its default value from false to true in your user settings.
Attributes Collapsing Horizontally or Wrapping Inconsistently Across Tags
Another frequent symptom occurs when your elements format correctly, but long lists of attributes either bunch up on a single line or break onto new lines against your preferred readability standards. The cause stems from default formatting thresholds where attributes are kept inline unless a specific count is exceeded, or where conflicting attribute positioning rules override your layout layout preferences.
To fix attribute wrapping inconsistencies, you can customize extension settings to dictate how elements handle parameters. By default, extensions like Pretty XML position the first attribute on the same line as the start element tag and can be configured using settings such as prettyxml.settings.attributesInNewlineThreshold, which sets the attribute count threshold required to position attributes in newlines. For example, if you set this threshold to 2, elements with two or fewer attributes keep them inline, while elements with three or more attributes push them onto separate, aligned lines. Furthermore, if certain structural nodes require custom behavior, you can define exceptions using prettyxml.settings.wildCardedExceptionsForPositionAllAttributesOnFirstLine, which accepts an array of strings representing wildcard patterns for element names to ignore when applying global attribute positioning rules. You can also adjust indentation width by modifying prettyxml.settings.indentSpaceLength, which defaults to 4 spaces per indentation level.
Syntax Errors and Missing Closing Tags Disrupting Formatting Routines
When attempting to beautify an XML document, you may encounter formatting failures, error popups, or code that refuses to align properly, accompanied by syntax warnings in your editor. The cause of this disruption is usually malformed markup, such as unclosed tags, unescaped characters, or missing root structures that prevent the parser from successfully building a document object model before formatting execution.
To resolve syntax-driven formatting failures, leverage the built-in validation and auto-completion features provided by your XML extension. The Red Hat XML extension features syntax error reporting via LemMinX, which flags errors in your documentation and provides quick fixes to resolve them. To prevent manual closing errors while authoring, ensure that tag auto-closing is active, which automatically inserts matching close tags when you type a forward slash in an opening tag. Additionally, you can enable simultaneous editing of opening and closing tag pairs by setting editor.linkedEditing to true in your settings.json file. If you are working with partial or invalid XML selections, Pretty XML also includes a beta format selection feature that indents selected blocks based on their initial line depth without requiring the entire document to be syntactically complete.
How to Confirm Your XML Formatting Configuration is Active and Working
If you have applied shortcuts and adjusted preferences but want absolute confirmation that your formatting setup operates correctly, you should run a controlled verification test. The cause of lingering doubts is usually a mismatch between the active document language mode and the formatter’s registered file associations.
To confirm everything works, create a test XML file and verify that the bottom-right status bar in Visual Studio Code displays ‘XML’ as the active language mode. If it shows ‘Plain Text’, click it and manually select ‘XML’. Next, paste unformatted markup into the editor and execute your formatting shortcut. For quick tasks or when working across multiple machines without extensions installed, you can also use our free XML Formatter tool online to instantly clean up markup before bringing it back into your workspace. Below is a worked verification example showing an unformatted input block and its properly indented output structure:
Input XML:
<Inventory><Item id="99" category="Hardware"><Name>Wrench</Name><Price>14.50</Price></Item></Inventory>
Output XML:
<Inventory>
<Item id="99" category="Hardware">
<Name>Wrench</Name>
<Price>14.50</Price>
</Item>
</Inventory>
Checking that your output matches this indented block confirms that your indentation spacing, tag nesting, and line breaks are functioning correctly under your configured editor rules.