Emacs command to add HTML tags
A while back I asked Jason Fruit how to add HTML tags to text in Emacs, something like the format painter in Microsoft applications. He said he didn’t know of anything and wrote the following macro for me.
text
(defun tag-word-or-region (tag)
"Surround current word or region with a given tag."
(interactive "sEnter tag (without <>): ")
(let (pos1 pos2 bds start-tag end-tag)
(setq start-tag (concat "<" tag ">"))
(setq end-tag (concat "</" tag ">"))
(if (and transient-mark-mode mark-active)
(progn
(goto-char (region-end))
(insert end-tag)
(goto-char (region-beginning))
(insert start-tag))
(progn
(setq bds (bounds-of-thing-at-point 'symbol))
(goto-char (cdr bds))
(insert end-tag)
(goto-char (car bds))
(insert start-tag)))))I added the following line to my .emacs file to bind Jason’s macro to the key sequence C-x t.
text
(global-set-key "C-xt" 'tag-word-or-region)To add a tag to a single word, place the cursor before or in the word and execute the command. To tag a block of text, select the text first then execute the command.