I have a file with xml all on one line. Does the functionality already exist to reformat this buffer to be somewhat user-readable?


6 Answers 6


Does the functionality already exist to reformat this buffer to be somewhat user-readable?

Of course, and you have plenty of options. I'd probably feed it to an external program using:

C-x h C-u M-| xmllint --format - RET

This program comes with libxml2. You could also use tidy. Here's a list of commandline xml formatting tools: https://stackoverflow.com/questions/16090869/how-to-pretty-print-xml-from-the-command-line

You could also do a search and replace and then indent:

M-% > < RET > C-q C-j < RET ! C-M-\

Neat trick: you can copy and paste the above string into M-: (eval-expression) like this:

(execute-kbd-macro (kbd "M-% > < RET > C-q C-j < RET ! C-M-\\"))
  • A nice answer, but be wary that the command as written now will replace the buffer content with the output from the shell command. This is because of the C-u prefix. Commented Mar 4, 2017 at 14:30

The built-in sgml-mode has a command to do this: sgml-pretty-print. If you're in nxml-mode it looks like you need switch to sgml-mode first. You could write a command to temporarily switch to sgml-mode, run pretty-print, then switch back to nxml-mode.

For example, here is a command that will pretty-print the region, optionally with auto-fill enabled:

(defun xml-pretty-print (beg end &optional arg)
  "Reformat the region between BEG and END.
    With optional ARG, also auto-fill."
  (interactive "*r\nP")
  (let ((fill (or (bound-and-true-p auto-fill-function) -1)))
    (when arg (auto-fill-mode))
    (sgml-pretty-print beg end)
    (auto-fill-mode fill)))
  • 6
    In Emacs 26.1, sgml-pretty-print works directly from the nxml mode. Commented Jul 6, 2020 at 11:55

Mark your xml and do:

M-x sgml-pretty-print

Or just run the command without a marked region to prettify the whole buffer.


write this into your ~/.emacs.d/init.el:

(require 'sgml-mode)

(defun ninrod/reformat-xml ()
    (sgml-pretty-print (point-min) (point-max))
    (indent-region (point-min) (point-max))))

reload emacs, then just call M-x reformat-xml on the badly formatted xml buffer.

source: https://davidcapello.com/blog/emacs/reformat-xml-on-emacs/

  • 2
    He's not asking for something to reformat it. He's asking for something to make it human readable.
    – RichieHH
    Commented Aug 24, 2020 at 17:34

In nxml-mode, C-x H will mark the whole buffer and TAB will indent the selection.


Following the hint of the answer above, and assuming you have tidy installed a variation could be:

`C-x h M-| tidy -quiet -xml -utf8 -indent -`

This will open a new buffer *Shell Command Output* instead of directly replacing the contents of the buffer. After checking the result, replace the old contents with the new with:

C-x h M-insert-buffer

choose the suggested default which will probably be *Shell Command Output*. You can save the command for later with a keyboard macro:

C-x ( C-x h M-| tidy -quiet -xml -utf8 -indent - C-x)
C-x C-k n pretty-xml

With this you can execute M-x pretty-xml to reformat the buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.