How To Create Dotfiles With Emacs

Make sure your business, agency, or reseller clients are always connected and powered on with our optimized Managed VPS Hosting.

If you have multiple configuration files in different places, they can grow to a point of becoming unmanageable. And they’re probably full of stray bits of code you’ve pasted in from different places, and you don’t remember what some of those code snippets even do.

Does this describe your situation?

  • Multiple config files spread across your system in various nooks and crannies
  • Full of code snippets you added over many years
  • Not sure what some of those snippets even do
  • Wishing you had properly commented each of those code blocks when you added them

Here’s the remedy you’ve been waiting for.

Emacs and bash setup file headers under the property drawer.

With org-mode in emacs, you can manage all of those code snippets for an unlimited number of configuration files, fully documented, so you know what every bit of code is doing.

In this article, we are going to save custom configurations for the “/.emacs” (emacs configuration) file and the “./bashrc” (bash configuration) file as examples. Why these two files? These files were chosen because:

  1. They are common to many systems
  2. They illustrate how you can send code (“tangle” it) to multiple files from one file

But you can adapt these steps to write your configurations to any file on your system (or even remote files).

The steps taken in this article, as is, can overwrite files in your system. If you follow the steps in this article, make sure to save backup copies of any file you wish to overwrite.

Using Configuration Files

For many of the programs you use everyday, there are preferences and settings you can configure for your personal use.

Many apps have a “preferences” pane, where you can punch in different values.

However, many apps don’t provide such a simple solution. Rather, they churn through different configurations files to set values.

Here are a few apps and utilities that you may want custom configurations for:

  • Bash
  • Nano
  • Vim
  • Emacs
  • xmonad window manager
  • Other local or remote user-executable files

Adding custom configurations for these programs may save you time, energy, and produce an overall superior working environment.

About Using Org Babel for Creating Dotfiles

Org-babel lets you do as the name suggests: manage different snippets of code written in different languages from a single master file. So you can have your lisp code for emacs, your Haskell code for xmonad, your shell script fu for bash, your custom python scripts—whatever you have—all in one place, fully documented in an easy markdown-like syntax.

Babel accomplishes this by parsing source blocks in an org mode document.

This can be especially helpful for replicating configurations across multiple servers in a private cloud.

Additional resources:

The typical org babel flow from one file to many.

So you can have source code from virtual any language, and once “tangled” these unique snippets of code can be sent to local and/or remote locations or run in place.

For the purposes of this tutorial, the code blocks are being tangled and sent to individual configuration files for emacs and bash.

Because the tangle operation can write files that don’t exist already, this makes it easy to re-create your work environment in multiple locations. Just carry one file with you from workstation to workstation. Likewise, you could version control your master file and host it remotely.

An Example Dotfiles Configuration

The first step is merely to create a file from which to manage your other files. You can name this file whatever you want, but for this example I’ll use “dotfiles.”

From your home directory, or whichever directory you prefer, create the master file:

touch dotfiles.org

Add this example configuration for a .emacs and .bashrc file:

#+title: Dotfiles

* Emacs
  :PROPERTIES:
  :header-args: :tangle .emacs
  :END:

Example emacs "auto-fill-mode" setting:

#+begin_src elisp
  (add-hook 'text-mode-hook 'auto-fill-mode)
#+end_src

* Bash
  :PROPERTIES:
  :header-args: :tangle .bashrc
  :END:

Some ls aliases:

#+BEGIN_SRC sh
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
#+END_SRC

Christopher's lobster prompt:

#+BEGIN_SRC sh
PS1='[🦞 W] > '
#+END_SRC

Now, when you run “org-babel-tangle” (mapped to C-c C-v t in emacs) these unique source code blocks will “tangle” to the files designated in the “PROPERTIES” drawer under “header-args.”

These file paths will be relative to the directory that holds the master file. So if you want to send these files to other directories on your system, make sure to print the full file path.

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!