Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to define a shortcut to enter XML tag pairs. Pressing key combination key1-key2 which should prompt for entering a tag name, .e.g. "foo", and automatically insert "<foo></foo>". How to do that?

Ideally I'd like the cursor to be automatically moved to be after the first >.

share|improve this question
up vote 2 down vote accepted

If you use YASnippet in xml-mode it already contains a snippet for the functionality you ask for.

Enter the following


press Tab and choose <tag>...</tag> for an inline XML tag or <tag>\n...\n</tag> for a tag on its own line and what you entered turns into





respectively. As what you have entered turns into either the name for the opening tag is marked and you can enter whatever tag name you want and it will copied to the closing tag too. When you are done entering the tag name you press Tab and the cursor is placed between the tags so that you can enter content to the tag. When you are done entering content in the tag you press Tab again to place the cursor after the closing tag.

Note that if you want to customize the behavior of these snippet it is easy. Use the YASnippet menu to find the snippets in question and then edit them to fit your needs.

To get the said snippets in any other mode you can create them for that mode by doing as follows:

  1. Add the following to your .emacs:

    (setq yas/root-directory "~/.emacs.d/mysnippets"); Develop and keep personal snippets under ~/emacs.d/mysnippets
    (yas/load-directory yas/root-directory); Load the snippets

    If you prefer to keep your snippet in another directory go ahead and choose another.

  2. Enter the mode you want the snippets in.

  3. Do M-x yas/new-snippet.
  4. Enter the name tag.1l
  5. You will now get a chance to edit the snippet. Mark everything by doing C-x h, then kill it by C-w.
  6. You will now have an empty snippet. Paste the following into it and make sure the snippet ends after $0 and not on a new line:

    #name: <tag>...</tag>
    #key: tag
    # --
  7. Do C-c C-c to save and load the snippet.

  8. Do step 3 to 7 but instead use the name tag.2l and instead paste in the following:

    #name: <tag> \n...\n</tag>
    #key: tag
    # --

Note that the key yasnippet uses for expansion, Tab, is bound to another function in markdown-mode by default. Thus you either have to use a fix such as or bind yas/expand to a key of your preference.

The following animation shows how the first snippet works:

Animation of the snippet <${1:tag}>$2</$1>$0

share|improve this answer
Your suggested trick works except that every time when I need to enter xml-mode first. How to make that more easy? How to define a short cut so I don't need to manually enter xml-mode? – qazwsx Mar 12 '12 at 3:04
@duperuser You may not want to enable that snippet for all modes but just for some modes. What modes do you want to use it in? Maybe you need a more effective way to enter xml-mode instead? – N.N. Mar 12 '12 at 7:46
I want to quickly insert pair of <tag></tag> when editting a markdown file in markdown-mode. – qazwsx Mar 13 '12 at 0:59
@duperuser I have added instructions for how to use it in markdown-mode. – N.N. Mar 13 '12 at 19:39
@duperuser If you want four spaces in tag.2l: 1) Enter markdown-mode, 2) M-x yas/find-snippets, 3) Choose tag.2l, 4) Make it so that there is four spaces before $2, 4) C-c C-c to save and load the snippet. – N.N. Mar 14 '12 at 6:54

It is possible and should be easy to write that.

But I think what you are after is snippet/skeletons. Please checkout yasnippet. Its basically a template system easily extensible to your needs

share|improve this answer
I'm using yasnippet in LaTeX, should be very helpful for this case as well. – Dror Feb 28 '12 at 8:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .