Instantly share code, notes, and snippets.

What would you like to do?
Sublime Text 3 plugin for formatting xml. Works on Sublime Text 2 as well. Created by André Bergonse
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 != b"":
self.view.set_status('xmllint', "xmllint: "+err)
self.view.replace(edit, self.view.sel()[0], result.decode('utf-8'))
def clear(self):

This comment has been minimized.

vladox commented Sep 17, 2013


I'm getting following error in the console when trying to apply the command without selecting any text:

File "/Users/myuser/Library/Application Support/Sublime Text 3/Packages/User/", line 13, in run
self.view.set_status('xmllint', "xmllint: " + err)
TypeError: Can't convert 'bytes' object to str implicitly

I can't understand that error since the content of err is:

b"-:1: parser error : Document is empty\n\n^\n-:1: parser error : Start tag expected, '<' not found\n\n^\n"

I'm testing using Mac OS X 10.8.5 do you see this in Ubuntu as well?



This comment has been minimized.

bastic commented Oct 29, 2013

@vladox in line 13, err hast to be set as string. therefore change:

self.view.set_status('xmllint', "xmllint: "+err)


self.view.set_status('xmllint', "xmllint: "+err.decode(encoding='UTF-8'))

however this will only get rid of the "TypeError:..." and not your real parse error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment