Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Format XML in Sublime Text 2 (OS X)
import sublime, sublime_plugin, subprocess
class TidyXmlLintCommand(sublime_plugin.TextCommand):
def run(self, edit):
command = "XMLLINT_INDENT=$'\t' xmllint --format --encode utf-8 -"
# help from
xmlRegion = sublime.Region(0, self.view.size())
p = subprocess.Popen(command, bufsize=-1, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, shell=True)
result, err = p.communicate(self.view.substr(self.view.sel()[0]).encode('utf-8'))
if err != "":
self.view.set_status('xmllint', "xmllint: "+err)
self.view.replace(edit, self.view.sel()[0], result.decode('utf-8'))
def clear(self):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment