Archive for the ‘editor’ tag
Pretty print XML files in Emacs
I have been using Emacs for most of my editing chores and recently I had to edit lot of XML files - most of which were machine generated without any space or indentation. I wanted someway to pretty print the XML file. Maybe I didn’t search very well, but today after getting my StackOverflow beta invite, I decided to post it there. I did get a good answer through it, but found another solution from Google.
It is an elisp function which you have to just copy-paste onto your .emacs file and reload emacs. After that, you can pretty print your XML files by marking the region and typing M-x bf-pretty-print-xml-region
Here is the function
(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!"))
By the way thanks to Marcel Levy for answering my first SO question. Now I really have to post a review about the beta.

