Created
July 26, 2011 05:13
-
-
Save alakra/1106022 to your computer and use it in GitHub Desktop.
Prettify XML with a key combo in Emacs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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!")) | |
;; map C-c C-c | |
(global-set-key (kbd "C-c C-c") 'bf-pretty-print-xml-region) | |
(lookup-key (current-global-map) (kbd "C-c C-c")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment