Elisp: HTML, Update Title

By Xah Lee. Date: . Last updated: .

Here's a command to update a existing HTML file's title.

(defun xah-html-update-title ( @title)
  "Update the <title>…</title> and <h1>…</h1> of current buffer.

URL `http://ergoemacs.org/emacs/elisp_update-html-title.html'
Version 2019-01-11"
  (interactive
   (let ($oldTitle)
     (save-excursion
       (goto-char 1)
       (re-search-forward "<title>\\([^<]+?\\)</title>")
       (setq $oldTitle (match-string 1 )))
     (list (read-string "New title:" $oldTitle nil $oldTitle "INHERIT-INPUT-METHOD"))))
  (let ($p1 $p2)
    (save-excursion
      (goto-char 1)
      (progn (search-forward "<title>")
             (setq $p1 (point))
             (search-forward "</title>")
             (search-backward "<")
             (setq $p2 (point))
             (delete-region $p1 $p2 )
             (goto-char $p1)
             (insert @title ))
      (xah-html-update-first-h1 @title))))
(defun xah-html-update-first-h1 ( @h1Text)
  "Update the first <h1>…</h1> of current buffer.

URL `http://ergoemacs.org/emacs/elisp_update-html-title.html'
Version 2019-01-11"
  (interactive
   (let ($oldTitle)
     (save-excursion
       (goto-char 1)
       (re-search-forward "<h1>\\([^<]+?\\)</h1>")
       (setq $oldTitle (match-string 1 )))
     (list (read-string "New title:" $oldTitle nil $oldTitle "INHERIT-INPUT-METHOD"))))
  (let ($p1 $p2)
    (save-excursion
      (goto-char 1)
      (if (search-forward "<h1>")
          (progn
            (setq $p1 (point))
            (search-forward "</h1>")
            (search-backward "<")
            (setq $p2 (point))
            (delete-region $p1 $p2 )
            (goto-char $p1)
            (insert @h1Text ))
        (progn
          (message "<h1> tag not found."))))))

When called, it'll prompt you for a new title. The update will happen on both the title tag and h1 tag.

You can give it a hotkey. [see Emacs: How to Define Keys]

This saves me some 20 keystrokes and manual eyeballing processes. Without this command, i have to first move to the “title” tag, edit there, copy it, then move to the “h1” tag, paste over.

You might modify the code for any programing files or blog that you frequently need to update part of it.

Elisp HTML Commands

  1. wrap-url
  2. URL to Link
  3. Link to Dead Link
  4. Make Image Links
  5. Extract URL
  6. Word to Wikipedia Linkify
  7. Wikipedia URL Linkify
  8. URL Percent Decode/Encode
  9. Lines to HTML Table
  10. Markup Function Names
  11. Color Source Code
  12. Transform Text Under Cursor
  13. Chinese Char Reference Linkify
  14. HTML make-citation
  15. update-title
  16. Google Map Linkify 🌐
  17. CSS Compressor
  18. Make Ruby Annotation
  19. Move Image File

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs

Emacs Lisp

Misc