A new XML mode

Emacs is distributed with a mode for editing XML files. It features syntax highlighting, indentation support and not much more (or maybe I'm just ignorant of its more advanced features). For editing XML in Emacs I can recommend a mode called nXML-mode. The features of nXML-mode that makes me prefer it is

You can download it from the authors site and install it by unpacking the .tar.gz and inserting something like the following in your .emacs file

(load-file "where you unpacked nXML/rng-auto.el")
(setq auto-mode-alist
    (cons '("\\.xml" . nxml-mode) auto-mode-alist))
(setq auto-mode-alist
    (cons '("\\.html" . nxml-mode) auto-mode-alist))

Where you replace the red text with the directory where you unpacked the nXML files.

The first line will set up auto loading of the needed elisp files. The second and third lines will make emacs use nXML mode for files whose names end with .xml and .html respectively. You probably won't want to use the third line if you edit HTML that isn't XHTML.


The validation features of nXML mode makes emacs validate the contents of your buffer against a given schema. The schema is described in the RELAX NG language. Schemas for the following formats are included in the nXML distribution

It is possible to define your own schemas, but I haven't needed to do it yet, so I can't show an example of this.

If you are writing XML which is not in one the formats defined above, and you don't want to define your own schema, the validation feature can still assist you in checking that your XML is well-formed.

Errors are highlighted with red underlining while text is being written. When you hover the mouse pointer above the underlined text a description of the encountered error is shown as a tool tip. You can also use the command "rng-next-error" which is by default bound to "C-c C-n", which will take the point to the next error and show the error description in the minibuffer.


There exists a number of completion/expansion systems for emacs, and nXML mode adds another one specifically for XML text.

The command "nxml-complete" can help complete tag and attributes names. When the command is run it will either insert as much text as possible, or - if no text can be inserted - open a completion buffer, and let you enter the tag/attribute in the minibuffer, with normal tab completion.

The "nxml-complete" command is by default bound to "C-return". If you can't get this key binding to work, you're probably using cua-mode, which will use this key combination for cua-set-rectangle-mark

Writing end tags

Writing XML documents can be somewhat tedious because you have to write the name of each tag twice. Most editors with some support for XML will help you with this, and emacs with nXML mode is no exception. nXML mode contains three function for writing end tags in a convenient way:

peter.peca.dk About Emacs Free Software Projects Linux

Valid XHTML 1.0 Strict