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
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)
Add a directory to my load path so that Emacs can find the package.
Load the package.
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?
It wraps all of my configuration for Tomatinho into one scope: the
use-packagemacro. I only need to look in one place for everything related to Tomatinho.
It loads the package. Notice that I am not using
load-file, et cetera.
It allows me to bind the
tomatinhocommand to a key which I name as if I were using the
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 tso that
use-packagewill ignore it. The latter option is nice because if I change my mind again and want Tomatinho back I only need to delete the
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
: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.