Using Obsidian with Termux and VIM

November 20 2020

In the latest chapter of getting my shit together, I've moved all my notes from Notion to Obsidian. In this post, I talk a bit about why I think it's cool, and how I use it on my mobile.

What is Obsidian?

Obsidian is the latest in the recent vogue of "Second Brain" apps. Essentially it's a place to take notes, and capture ``[[relationships]]` between those notes, resulting in something like your own personal Wikipedia.

My knowledge graph in Obsidian

How do I use it?

  1. Daily Notes: this is where I spend most of my time, capturing anything interesting that happened that day, tasks that come up etc. If something warrants more thought, I'll later turn it into a note of it's own.
  2. Work: I'll usually keep note files for people (to store notes from 1-1s, anything interesting about them), projects, and occasionally meetings - relating them all together. I'll also create separate notes for any opnions that I'm forming.
  3. Learning: anything I'm learning about, or reading/watching that I might want to remember later will have a note.

I don't really know if it's zettelkasten, evergreen notes, or GTD, or bullet journaling. It doesn't really matter.

Why switch from Notion?

Notion is great, and definitely does a lot of things better than Obsidian does (particularly in the project management space). I had an awfully elaborate setup with Notion, with all sorts of relational databases. I found that this added a lot of overhead, both for capturing notes and navigating them.

The selling point of Obsidian for me was that all the notes are (more or less) regular markdown, and stored on plain text files on your own hard drive. There was something rather romantic about the idea of actually owning your own thoughts. Even if the app disappears one day, my notes will still be there in a usable format.

This emphasis on just writing text, and navigating organically through the graph feels like it's led to me writing more.

What about mobile?

The caveat was that Obsidian currently only has a desktop app. It's on their roadmap, which the two-person team seem to be blistering through.

However the beauty of having plain text files is that I can do whatever I want with them. This is how I've set up my mobile note-taking.

Syncthing

To get the files onto my phone (and between my laptops), I use an open source app called Syncthing. This works a bit like rsync, and does it's best to keep the files in sync across any connected devices. As most of my notes are small, self-contained files, conflicts tend to be quite rare.

Termux

While there are a handful of markdown editing apps available on Android, I prefer to stick with the soothing familiarity of VIM to do my editing. To achieve this, I use an incredible app called Termux.

Termux is a terminal emulator with a large collection of Linux packages - which allows you to use things like git, node.js, ssh, and VIM all from the comfort of your tiny phone.

termux-setup-storage
pkg update
pkg install vim

With these three commands, I was able to setup Termux to be able to see my synced vault folder, and edit with VIM.

(*Okay I also installed git and cloned my dotfiles and installed all my regular VIM plugins. But you get the gist.)

One slight annoyance is the the folder will be in different places on each device. To handle that, I have a .extra file to keep environment-specific config, to which which I've added:

export KB_DIRECTORY=/storage/shared/kb

VIM

While it was great to edit the files in VIM, without the graph view and backlink support it was a lot harder to navigate..

Fortunately, with a few additional lines to .vimrc, we can make this easier!

autocmd BufNewFile,BufRead *.md setlocal filetype=markdown
autocmd BufRead,BufNewFile $KB_DIRECTORY* setlocal path+=$KB_DIRECTORY/**
set suffixesadd+=.md

These three lines do the following:

  1. Set the filetype to markdown for md files, which gives us some syntax highlighting
  2. If I'm in any file in the knoweldge base, set my path to include all subdirectories of that folder. This means that when using gf to "go to file under cursor", or using :find to find a file, we can look at all then notes in any of my subdirectories.
  3. Add .md to the list of "default" suffixes. When using gf to navigate to a file, VIM will try all theses suffixes as it looks for the file in the path.

The combination of the above means that if I have a [[Backlink]] that points to /SubFolder/Backlink.md, I can put my cursor over the [[Backlink]], hit gf and VIM will be able to navigate to that file. Success!

Termux API & Widget

So I now have the ability to edit my notes on my phone, but it's still a bit of a faff. Ideally on a mobile, I'd want to be able to quickly capture a note, and easily find a particular note to refer to.

The first thing I want is the ability to capture a note quickly. As the notes are plain text files, I can easily manipulate them with bash.

I have a bash function that will find or create the daily note file for today's date, and append whatever I type as a bullet to that file.

today() {
	now=$(date +"%Y-%m-%d");
	if ([[ -z $1 ]]); then
		vim "$KB_DIRECTORY/Daily/$now.md"
	else
		echo $"- $@" >> "$KB_DIRECTORY/Daily/$now.md"

	fi
}

This allows me, wherever I am in the CLI, to type today I did some stuff, and it will add that to my note!

I have a similar (imperfect) function to be able to fuzzy find any note in my knowledge base:

kb() {
	existing=$(find $KB_DIRECTORY -iname \*"$*"\*.md | head -n 1)
	if ([[ -n "$existing" ]]); then
		vim "$existing"
	else
		vim "$KB_DIRECTORY/$*.md"
	fi
}

This function allows me to type kb obsidian and it will find the first match it finds with obsidian in the name.

That's all well and good, but still involves opening up Termux and typing a bunch a stuff in. Termux has some additional packages that can be installed, which further integrate it with the Android OS.

In particular, I use Termux API, and Termux Widget.

pkg install termux-api

Termux allows you to create bash scripts in ~/.shortcuts/ (or ~/.shortcuts/tasks for background tasks), which can then be accessed from a homescreen widget. And with Termux API, we can use some of the OS's input facilities. So, I can have a shortcut like the below:

#!/data/data/com.termux/files/usr/bin/bash
source ~/.functions
source ~/.extra

result=$(termux-dialog -t "Note" | jq -r '.text');

today "$result"

This script opens up a Dialog box which lets me type in a note, uses jq to parse the JSON result of that dialog and extract the text, and uses the today bash function from earlier to capture it in my daily note!

Even better, we can do the same thing with a voice note!

result=$(termux-dialog speech -t "Note" | jq -r '.text');

The Result

Behold, in all it's nerdy goodness: