Literate Dotfiles

The Irreal blog has recently published a post about Managing dotfiles with Org. The idea is simple: Put your configuration files and other scripts in a central (version controlled) place and use Emacs to create the "real files" based on the content of your org file.

Here's a short example:

* Git

#+BEGIN_SRC conf :tangle "~/.gitconfig" :padline no
    email =
    name = Max Mustermann

    showUntrackedFiles = all

    lg = log --color --graph --oneline --decorate --abbrev-commit

#+BEGIN_SRC fundamental :tangle "~/.gitmessage" :padline no
  # * Why was this change necessary?
  # * How does it address the problem?
  # * Are there any side effects?
  # * What other options did you consider?

* Bash

#+BEGIN_SRC sh :tangle "~/bin/my-hello" :shebang "#!/bin/bash" :mkdirp yes
  set -euo pipefail

  echo hello

Executing the Emacs function (org-babel-tangle) will create three files:

Oh and yes, I can use the same solution on Windows and on Linux. Dealing with configuration or code in org-mode is also delightful: I can narrow the buffer to only show a specific file and I'll even get real syntax highlighting.

One last trick, just for fun: (org-babel-tangle) can be called via the command line:

emacs --batch -l org --eval '(org-babel-tangle-file "")'

Published: 2019-08-10