Since I deal with many SOAP web services for work, I also have to deal with lots
of XML files. In the application server log they usually appear on one line,
which makes the reading unnecessarily painful. Fortunately, Emacs coupled with
the handy xmllint
comes to the rescue.
First, I installed xmllint
with: sudo apt-get install libxml2-utils
Now I could format my XML buffers with: C-x h C-u M-|
xmllint -format -
RET
However, this is Emacs, so there is always a more productive way to solve
problems. Why should I have to input the options for xmllint
or remember the
necessary key bindings all the times?
(defun mu-xml-format ()
"Format an XML buffer with `xmllint'."
(interactive)
(shell-command-on-region (point-min) (point-max)
"xmllint -format -"
(current-buffer) t
"*Xmllint Error Buffer*" t))
Since I only need this utility in XML buffers, I bound it for nxml-mode-map
.
(use-package nxml-mode ; XML editing
:mode "\\.xml\\'"
:bind (:map nxml-mode-map
("C-c m f" . mu-xml-format))
…