How to indent XML files automatically when opened in Sublime Text 2?
XmlSublimetext2IndentationXml Problem Overview
I'm using Sublime Text 2 (with the Indent XML plugin) for editing XML files.
How can I configure Sublime Text to automatically execute the "Indent XML" action right after opening a file named *.xml?
Xml Solutions
Solution 1 - Xml
To indent / prettify / beautify XML, I use SublimeText Indent plugin:
- Install the package with Package Control (search "indentxml").
- Open any XML file, or create a new file and paste any XML into it.
- Ctrl-K, F to indent it.
Solution 2 - Xml
Try this plugin:
-
Tools > New Plugin
-
Copy and paste code below
-
Save in the Packages/User directory with a .py extension (should be the default directory)
-
Open any XML file and it should run. [Open console (Ctrl+~ in Windows) to see any errors]
The code supports both "Indent XML" and "IndentX". If they don't exist I believe the command fails silently and shouldn't affect anything. I'm a plugin newbie but hope this helps!
# Packages/User/AutoIndent.py
import sublime, sublime_plugin
class OnOpenCommand(sublime_plugin.EventListener):
def on_load(self, view):
if view.file_name().lower().endswith(".xml") :
#print "{0}: Auto indenting {1} with Indent XML's auto_indent command".format(__file__, view.file_name())
view.run_command("auto_indent")
#print "{0}: Auto indenting {1} with IndentX's basic_indent_tags command".format(__file__, view.file_name())
view.run_command("basic_indent_tags")