Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

In Emacs, how to define a keyboard shortcut to quickly enclose highlighted text with <tag> and </tag>, optionally prompting for inputting the tag name tag?

share|improve this question
up vote 2 down vote accepted
(defun my-tagger (tag)
  (interactive "sTag: ")
  (if (not (use-region-p))
      (error "no region is highlighted")

    (let ((text (buffer-substring (region-beginning) (region-end))))
      (delete-region (region-beginning) (region-end))
      (insert (format "<%s>%s</%s>" tag text tag)))))

(global-set-key (kbd "<f7>") 'my-tagger)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.