How to Edit XML Files With nXML Mode in Emacs

Edit XML with nXML mode

XML files have become an important part of the web landscape. There may be a time, or there has already been a time, in which you need to edit one of these files in a meaningful way. For users of the Emacs text editor, there is an easy-to-use built-in editing mode just for XML files. This nXML mode makes it so much easier to edit XML files that you’ll wonder how you went without it for so long.

XML comes with a variety of different “schema” for how the content is organized. The good news is that you can use nXML mode for editing any kind of schema you need.

Get a better overview on the differences between VPS vs cloud hosting, and why it matters.

What is nXML Mode

nXML mode is a built-in major mode for the Emacs text editor which lets you more easily edit XML files. This mode provides:

  • Syntax highlighting of tags, which makes it easier to identify the tags and schema the file is using
  • Auto completing of code
  • Features for identifying different schema in existing files

How To Activate nXML Mode

This mode is built into Emacs already, so if you open an XML file on your server Emacs will be able to auto-detect the mode listing for XML files and automatically turn on nXML mode.

Quick Tagging

Editing XML files is especially time-consuming because of the insertion of multiple tags. Just like an HTML file, an XML file assigns meaning to different pieces of content by enveloping them in tags that typically begin with < and end in >. With xXML mode you can easily auto-complete an opening tag with an “end tag” by providing a few simple keystrokes.

Auto Insert End Tag On Line

The first option you have for enclosing a tag basically gives the end tag on the same line and places your cursor between the opening and closing. For example, if you have a tag for “date”, you can start typing it like so:

<date

With your cursor landing after the “e” in date, you can simply type C-c C-i.

Your final output will look like this:

<date></date>

You will notice you cursor placed between the opening and closing tag. Remember, in Emacs, the key you enter is placed behind the character on which your cursor is resting. This means your cursor will be resting on the < in </date>. Just start typing and your content will be placed in between the two tags.

Line Break Between Tags

For some users it would be preferable to have their cursor rest between the two tags on a new line. In order to set your tags apart with a line break between them, you can run theC-c C-b command. Your result will look like this:

<date>

</date>

You will notice you cursor placed between the opening and closing tag, but this time it will be on its own line. Remember, in order to run this command properly your must be initially resting on the opening tag itself.


Well done! You now know how to use nXML mode in Emacs. For other information about how you can expand and go in more depth on nXML mode, check out the info page, which should already be installed in your system.

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!

Server Madness Sale
Score Big with Savings up to 99% Off

X