15

I write and publish my blog with org-mode and org-page. The thing is that I want to write about Emacs and, I would like to have the keystrokes highlighted as Stack Exchange does with the <kbd> tag. Getting this: C-x C-f instead of something else!

In which way or how could I achieve this?

14

Here’s what I use for my blog.

(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* ((is-org-mode (derived-mode-p 'org-mode))
         (tag (if is-org-mode
                  "@@html:<kbd>%s</kbd>@@"
                "<kbd>%s</kbd>")))
    (if (null (equal key "\r"))
        (insert
         (format tag (help-key-description key nil)))
      (insert (format tag ""))
      (forward-char (if is-org-mode -8 -6)))))

Invoke it by calling C-c k.

  1. It will prompt you for a key just like C-h k would do.
  2. It inserts that key sorrounded in escaped <kbd> tags.
  3. If you just hit RET, it will insert the tags and leave point inside (instead of inserting any key), so you can type more complicated keys.
  4. It even has some code to make it usable outside of org-mode!
  • 1
    your blog seems cool, btw. You write it on org-mode right? how do you publish it? – shackra Oct 15 '14 at 19:43
  • 2
    @JorgeArayaNavarro I use a heavily patched version of ox-jekyll. I've been meaning to blog about it, but the patch has 200 lines of code, so it's proven to be a bit of a challenge. – Malabarba Oct 15 '14 at 19:46
  • 1
    What's the difference between (define-key org-mode-map (kbd "C-c k") #'endless/insert-key) and (define-key org-mode-map (kbd "C-c k") 'endless/insert-key)? – shackra Oct 15 '14 at 23:40
  • 2
    @JorgeArayaNavarro See my answer to this comment – Malabarba Oct 15 '14 at 23:43
  • 2
    Nice. And if you want each individual key in the sequence as a separate tag, just change (help-key-description key nil) to (mapconcat 'identity (split-string (help-key-description key nil)) "</kbd><kbd>") – phils Nov 19 '14 at 4:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.