Nathan Knowler

Setting Your nb Notebook Based on Context With direnv


I use nb for taking notes. I love that it allows me to write with Neovim, my text editor of choice. It also can sync to a Git repository which is nice for revision control.

As much as I love nb, I’m not the most consistent with it. Sometimes I use the Apple Notes app or iA Writer since they both sync with my phone. Another hurdle is remembering to change which notebook I am writing in depending on the context. I end up writing a note in the wrong notebook, which can put me off from using the tool.

Today, I realized a nice little trick to do this for me. It involves another tool I use daily. direnv is a shell extension that loads and unloads environment variables based on your current directory. In addition to setting environment variables, it can run commands.

Inside the .envrc file for the context I’m working in (e.g. a repo or topical parent directory), I run the nb use command with the name of the appropriate notebook. For example, for my website, where I’m generally doing a lot of thinking about the Web, I set it to my magic-paper notebook. After editing the .envrc file, I give direnv permission to run the edited file using direnv allow and that’s it!