Jumping to use-package Blocks in Your Emacs Configuration

Jordon Biondo published a useful gist that allows you to easily navigate to use-package blocks in your .emacs or init.el file. If you have lots of packages—and therefore lots of use-package blocks—you’ll find it useful.

Biondo recommends that you install imenu-anywhere to make the most use of the hack and I agree. Without imenu-anywhere, you have to first ask imenu for Used.Packages and then the particular block you want. With imenu-anywhere you just specify the block. You also get the choices in ido (or Helm if you have it installed) instead of just a plain prompt. You can probably get imenu to use ido as well but imenu-anywhere takes care of that for you.

Biondo doesn’t say how he invokes his code and that turns out to be a little tricky because imenu-generic-expression= is a local variable. I solved that problem by invoking it through the emacs-lisp-mode-hook. I implemented it like this (based on a comment on reddit by xuchunyang):

(use-package imenu-anywhere
  :ensure t
  :init (global-set-key (kbd "C-.") 'imenu-anywhere)
  :config (defun jcs-use-package ()
            (add-to-list 'imenu-generic-expression
             '("Used Packages"
               "\\(^\\s-*(use-package +\\)\\(\\_<.+\\_>\\)" 2)))
  (add-hook 'emacs-lisp-mode-hook #'jcs-use-package))

This bit of Elisp turns out to be pretty handy—I became addicted almost immediately—so you should consider whether or not it will improve your workflow. In either case, imenu-anywhere is a clear win.

This entry was posted in General and tagged . Bookmark the permalink.