Emacs: Jump to Info from Command Documentation

One of the coolest things about Emacs is that it’s self-documenting. What this means is that for any piece of functionality in Emacs, we can ask the editor about that thing. We can ask it to tell us about any function (C-h f), variable (C-h v), key binding (C-h k) or even the mode(s) we’re currently in (C-h m). Most of the time this documentation is comprehensive and heavily cross-referenced. If you’re not using these commands, you absolutely should start today.

Emacs also comes bundled with a detailed user manual (C-h r – think, read the manual), which is important when you don’t know exactly what you’re looking for, or if you want to see a command in the context of related functionality. Maybe you know about kill-word and kill-sentence but they aren’t exactly what you’re looking for – the manual is the first place to check to learn about (say) kill-sentence or kill-sexp.

Somewhat less well-known is the handy Info-goto-emacs-command-node command (on C-h F) which will take you directly to the Emacs manual page that talks about a given command. But unless we’re coding in Elisp, most of the time we work in terms of keystrokes and not command names. Wouldn’t it be more useful if we could look at the documentation for given key sequence and then, if we wanted more information, easily jump to the corresponding manual page? Let’s code that up:

(defun camdez/Info-goto-from-command-help ()
  "Go to the Info node in the Emacs manual for the command
currently being viewed in `help-mode'."
  (interactive)
  (let ((help-function (car help-xref-stack-item))
        (help-symbol (cadr help-xref-stack-item)))
    (if (and (eq help-function 'describe-function)
             (commandp help-symbol))
        (Info-goto-emacs-command-node help-symbol)
      (error "Info is only available for commands"))))

(Note that we have to do a couple tests on help-xref-stack-item because we can only jump into the manual for commands, and not for arbitrary functions or variables.)

Now let’s bind that to the (previously unused) i key in help-mode:

(eval-after-load 'help-mode
  '(define-key help-mode-map "i" 'camdez/Info-goto-from-command-help))

And we’re done! After putting that in your .emacs file and restarting Emacs (or reloading the config), try it out with something like C-h k C-k (“what does the C-k keystroke do?”) and then press i in the *Help* buffer.