Si sólo necesita bastante sangría sin introducir ningún nuevo saltos de línea, se puede aplicar el comando indent-region
a toda la memoria intermedia con estas teclas:
C-x h
C-M-\
Si también necesita introducir saltos de línea, para que las etiquetas de apertura y cierre estén en líneas separadas, puede usar la siguiente función de elisp muy agradable, escrita por Benjamin Ferrari. Lo encontré en su blog y espero que sea bien para mí para reproducir aquí:
(defun bf-pretty-print-xml-region (begin end)
"Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this. The function inserts linebreaks to separate tags that have
nothing but whitespace between them. It then indents the markup
by using nxml's indentation rules."
(interactive "r")
(save-excursion
(nxml-mode)
(goto-char begin)
(while (search-forward-regexp "\>[ \\t]*\<" nil t)
(backward-char) (insert "\n"))
(indent-region begin end))
(message "Ah, much better!"))
Esto no se basa en una herramienta externa como ordenado.
A finales de 2013 tidy.el Versión: 20111222.1756 falla al ejecutarse en Emacs 24 con '' 'argumento de tipo incorrecto: stringp, nil''' – keiw
@keiw Eso es probablemente porque lo estás haciendo en un búfer que no tiene un nombre de archivo. Obtuve el mismo error y lo remonté a eso de mi lado al menos. – Alf