In Emacs, how to define a keyboard shortcut to quickly enclose highlighted text with <tag> and </tag>?

4

2

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?

qazwsx

Posted 2012-03-22T04:00:51.587

Reputation: 6 739

Answers

2

(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)

Tom

Posted 2012-03-22T04:00:51.587

Reputation: 1 841