Emacs: HTML, Delete Tags

By Xah Lee. Date: . Last updated: .

Here's a command to delete html tag pairs.

(defun xah-html-delete-tag-pair ()
  "Remove the next or current tag(s) under cursor.
The tags removed is the first angle bracketed tag to the right of cursor. Both begin and end tags are removed, unless it's a self-closing tag such as <br />.
URL `http://ergoemacs.org/emacs/emacs_html_delete_tags.html'
Version 2021-01-03 2021-06-08"
  (interactive)
  (require 'sgml-mode)
  (save-excursion
    (let (p0 inEndTag-p p1 p2 openTag-p1 openTag-p2 selfCloseTag-p closingTag-p1 closingTag-p2 openTagStr closeTagStr)
      ;; (progn
      ;;   (setq p0 (point))
      ;;   (search-backward "<")
      ;;   (setq p1 (point))
      ;;   (forward-char 1)
      ;;   (setq inEndTag-p (char-equal (char-after ) ?/))
      ;;   (search-forward ">")
      ;;   (setq p2 (point)))
      (progn
        (setq p0 (point))
        (search-forward ">" )
        (setq p2 (point))
        (search-backward "<")
        (setq p1 (point))
        (forward-char )
        (setq inEndTag-p (char-equal (char-after ) ?/)))
      (if inEndTag-p
          (progn
            (setq closingTag-p1 p1)
            (setq closingTag-p2 p2)
            (goto-char (1+ p1))
            (sgml-skip-tag-backward 1)
            (setq openTag-p1 (point))
            (search-forward ">" )
            (setq openTag-p2 (point))
            (setq closeTagStr (buffer-substring closingTag-p1 closingTag-p2))
            (setq openTagStr (buffer-substring openTag-p1 openTag-p2))
            (message "Deleted: %s%s" openTagStr closeTagStr)
            (delete-region closingTag-p1 closingTag-p2)
            (delete-region openTag-p1 openTag-p2))
        (progn
          (setq selfCloseTag-p (char-equal (char-after (- p2 2)) ?/))
          (if selfCloseTag-p
              (progn
                (setq openTagStr (buffer-substring p1 p2))
                (message "Deleted:\n%s" openTagStr )
                (delete-region p1 p2))
            (progn
              (setq openTag-p1 p1)
              (setq openTag-p2 p2)
              (goto-char (1+ p1))
              (sgml-skip-tag-forward 1)
              (setq closingTag-p2 (point))
              (search-backward "<" )
              (setq closingTag-p1 (point))
              (setq closeTagStr (buffer-substring closingTag-p1 closingTag-p2))
              (setq openTagStr (buffer-substring openTag-p1 openTag-p2))
              (message "Deleted:%s%s" openTagStr closeTagStr)
              (delete-region closingTag-p1 closingTag-p2)
              (delete-region openTag-p1 openTag-p2))))))))

HTML

Tags

Links

CSS

Misc

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial