Geert Schuring
Geert Schuring

Reputation: 2163

How to indent XML files automatically when opened in Sublime Text 2?

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?

Upvotes: 74

Views: 110882

Answers (3)

Zaiullah Ali
Zaiullah Ali

Reputation: 1

Here are the XML files:

HDR.xml

<?xml version="1.0" encoding="UTF-8"?>
<HDR>
  <enabled>true</enabled>
  <format>HDR10</format>
  <color_space>BT.2020</color_space>
  <transfer_function>ST.2084</transfer_function>
  <max_cll>1000</max_cll>
  <max_fall>100</max_fall>
</HDR>

Smooth.xml

<?xml version="1.0" encoding="UTF-8"?>
<Smooth>
  <enabled>true</enabled>
  <filter>GAUSSIAN</filter>
  <strength>0.5</strength>
  <radius>5</radius>
</Smooth>

Combined.xml

<?xml version="1.0" encoding="UTF-8"?>
<Settings>
  <HDR>
    <enabled>true</enabled>
    <format>HDR10</format>
    <color_space>BT.2020</color_space>
    <transfer_function>ST.2084</transfer_function>
    <max_cll>1000</max_cll>
    <max_fall>100</max_fall>
  </HDR>
  <Smooth>
    <enabled>true</enabled>
    <filter>GAUSSIAN</filter>
    <strength>0.5</strength>
    <radius>5</radius>
  </Smooth>
</Settings>

To save:

  1. Copy desired XML code.
  2. Open text editor (Notepad, TextEdit).
  3. Paste code.
  4. Save with .xml extension.

Upvotes: 0

Adi Sutanto
Adi Sutanto

Reputation: 1477

To indent / prettify / beautify XML, I use SublimeText Indent plugin:

  1. Install the package with Package Control (search "indentxml").
  2. Open any XML file, or create a new file and paste any XML into it.
  3. Ctrl-K, F to indent it.

Upvotes: 111

thewheat
thewheat

Reputation: 988

Try this plugin:

1) Tools > New Plugin

2) Copy and paste code below

3) Save in the Packages/User directory with a .py extension (should be the default directory)

4) 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")

Upvotes: 9

Related Questions