Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pretty printing XML files on Emacs

Tags:

emacs

editor

xml

I use emacs to edit my xml files (nxml-mode) and the files were generated by machine don't have any pretty formatting of the tags.

I have searched for pretty printing the entire file with indentation and saving it, but wasn't able to find an automatic way.

Is there a way? Or atleast some editor on linux which can do it.

like image 241
cnu Avatar asked Aug 15 '08 17:08

cnu


2 Answers

You don't even need to write your own function - sgml-mode (a gnu emacs core module) has a built-in pretty printing function called (sgml-pretty-print ...) which takes region beginning and end arguments.

If you are cutting and pasting xml and you find your terminal is chopping the lines in arbitrary places you can use this pretty printer which fixes broken lines first.

like image 93
Juan Garcia Avatar answered Sep 18 '22 14:09

Juan Garcia


If you only need pretty indenting without introducing any new line-breaks, you can apply the indent-region command to the entire buffer with these keystrokes:

C-x h C-M-\ 

If you also need to introduce line-breaks, so that opening and closing tags are on separate lines, you could use the following very nice elisp function, written by Benjamin Ferrari. I found it on his blog and hope it's ok for me to reproduce it here:

(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") (setq end (1+ end)))     (indent-region begin end))   (message "Ah, much better!")) 

This doesn't rely on an external tool like Tidy.

like image 22
Christian Berg Avatar answered Sep 18 '22 14:09

Christian Berg