Simplify Emacs Configuration With Use-Package

If you use GNU Emacs you will eventually find yourself with a lengthy .emacs file. Sites like MELPA and the Emacs Wiki will introduce you to a plethora of ways to configure and customize Emacs for your needs. But adding package after package can make your configuration file unwieldy in a hurry. So today I want to introduce you to a library that helped me to simplify and organize my .emacs file: use-package by John Wiegley.

A Simple Example

Here is a snippet from my .emacs prior to recently rewriting it:

(add-to-list 'load-path "~/.emacs.d/tomatinho")
(require 'tomatinho)
(global-set-key (kbd "<f9>") 'tomatinho)

This sets up Tomatinho, a nice and simple utility for using the Pomodoro Technique. The code represents the most common pattern in my .emacs:

  1. Add a directory to my load path so that Emacs can find the package.

  2. Load the package.

  3. Bind one or more keys to commands from the package.

This is how I load Tomatinho now:

(use-package tomatinho
  :load-path "tomatinho/"
  :bind ("<f9>" . tomatinho))

Nothing is different in terms of lines of code. So what does use-package do for me here?

  1. It wraps all of my configuration for Tomatinho into one scope: the use-package macro. I only need to look in one place for everything related to Tomatinho.

  2. It loads the package. Notice that I am not using require, load-file, et cetera.

  3. It allows me to bind the tomatinho command to a key which I name as if I were using the kbd macro.

  4. If I decide to stop using Tomatinho I only need to remove code from one place (cf. the first point), or I can add :disabled t so that use-package will ignore it. The latter option is nice because if I change my mind again and want Tomatinho back I only need to delete the :disabled t part.

I gain simplicity. With use-package I can make changes to how I use Tomatinho more simply than with my original configuration. But I understand if you see nothing convincing in this example. So let me show you an example of some other benefits to this structure.

A Longer Example

This is how I load Lua Mode:

(use-package lua-mode
  :config (progn
            (setq lua-indent-level 4)
            (use-package lua-block
              :config (lua-block-mode t)))
  :mode (("\\.rockspec" . lua-mode)
         ("\\.busted" . lua-mode)
         ("\\.slua" . lua-mode)))

Sometimes I want to configure Emacs to use a certain mode for files with names that match certain patterns. Normally I would do this by modifying the auto-mode-alist variable. But thanks to use-package I can make that same modification more tersely via the :mode keyword.

The :config keyword lets me tell Emacs what to do after it loads Lua Mode. I do two things: I configure the value of a variable that is special to Lua Mode, and I load an additional package. I could load lua-block elsewhere, but doing so within this :config section continues to help me keep all of my Lua settings in one place.

This example builds on what I feel is the biggest benefit of the use-package macro: it keeps all code related to a package in one place. This organization is invaluable when your .emacs grows to be hundreds or thousands of lines long. The macro also simplifies the syntax for binding keys, modifying the load path, associating modes with types of files, using interpreters with programming languages, delaying the loading of certain packages until Emacs loads a ‘parent’ package, and more.

So check out use-package to see its other features. Keep it in the back of your mind for when you get that creeping feeling that you ought to refactor your entire .emacs file. It shrunk the size of my .emacs and improved the organization without sacrificing any functionality. And it can do the same for you.


5 thoughts on “Simplify Emacs Configuration With Use-Package

  1. First of all I want to say great blog! I had a quick question that I’d like to ask if you don’t mind.

    I was interested to find out how you center yourself and clear your
    mind prior to writing. I have had a tough time clearing my thoughts in getting my thoughts out.
    I do take pleasure in writing however it just seems like the first 10 to 15 minutes tend to be lost just trying to figure out
    how to begin. Any recommendations or hints? Thank you!

  2. Thanks for your post. The use-package package itself has to be loaded first the usual way, right? (I mean, adding its path to load-path and then requiring it).

Add Your Thoughts

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s