Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Inserting the kbd tag in interactively

It doesn't take a psychic to guess the <kbd> tag will be useful when writing about Emacs. When Jorge Navarro asked for the best way to do this, over at Emacs.SE, I thought I'd share the snippet I use.

This simple command will ask for a key sequence (just like C-h k would) and will insert it for you. If you want to write the sequence manually, just hit RET when prompted. C-c k feels like a nice binding for it.

(eval-after-load 'ox-html
  ;; If you prefer to use ~ for <code> tags. Replace "code" with
  ;; "verbatim" here, and replace "~" with "=" below.
  '(push '(code . "<kbd>%s</kbd>") org-html-text-markup-alist))

(define-key org-mode-map "\C-ck" #'endless/insert-key)
(defun endless/insert-key (key)
  "Ask for a key then insert its description.
Will work on both org-mode and any mode that accepts plain html."
  (interactive "kType key sequence: ")
  (let* ((orgp (derived-mode-p 'org-mode))
         (tag (if orgp "~%s~" "<kbd>%s</kbd>")))
    (if (null (equal key "\C-m"))
        (insert 
         (format tag (help-key-description key nil)))
      ;; If you just hit RET.
      (insert (format tag ""))
      (forward-char (if orgp -1 -6)))))

It should work in both org-mode and html-like modes.

Update <2014-11-30 Sun>

A big kudos to u/abo-abo, for suggesting the use of ~.

comments powered by Disqus