grm blog. Work is copyrighted unless otherwise stated.
2022-12-02 Fri
^

Emacs config hygiene

Tips to keep a stelar, lean and clean, emacs configuration.

Use init.el

Inside the user-emacs-directory is where the config should lie.

Use(-)package

Use package helps with keeping the package definitions atomic plus it helps with installing the package if it's not already installed.

Besides any best efforts some packages will inevitably modify other package's config (e.g. adding hydras to other packages). This is why the order each package is installed and configured matters. use-package can help with that.

Modular config

Don't configure anything important in init.el, instead use something like this to load the rest of your config from somewhere else.

;; Load everything under ~/.emacs.d/config
;; as long as it ends with `.el' an elisp file
(let ((dir (concat user-emacs-directory "config/")))
  (mapc 'load-file
        (directory-files dir t ".el$")))

Use NN_name.el were NN is a number [00-99] to enforce ordering between configs. Like so:

00_core.el
09_theming.el
10_emacs.el
20_completions.el
30_org.el
50_dired.el
51_main.el
60_modes.el
90_mail.el
99_utilities.el

Check for dangling packages

Using use-package with everything enables this beauty of an oneline:

cd ~/.emacs.d/elpa
find . -maxdepth 1 -type d |\
    sed 's/^\.\///' |\
    grep -e '[0-9]' |\
    sed "s/\(.*\)-[0-9\.-]*$/grep -qHR 'use-package \1' ~\/\.emacs\.d\/config/" |\
    xargs -d $'\n' sh -c 'for arg do bash -c "$arg" || echo "$arg"; done' _ |\
    sed "s/.*use-package \(.*\)'.*/\1/"

that will list all packages installed in elpa/ and don't have a use-package definition in my config.