Build a FOSS Second Brain in Vim

Meet Gregory, a writer and the brains behind Face Dragons. He's the go-to guy for getting things done. Gregory's been living the digital nomad life in Asia for as long as anyone can remember, helping clients smash their goals. He writes on topics like software, personal knowledge management (PKM), and personal development. When he's not writing, you'll catch him at the local MMA gym, nose buried in a book, or just chilling with the family.

Second brains are in vogue, and so is second brain software. But you might not be willing to move away from software you already know so well, such as vim. Instead, you hope there is a way to get all the functionality of a personal knowledge management system while using free and open-source software. Building a second brain in vim isn’t very challenging.

Why Build a Second Brain in Vim?

If you found this article, you probably already know why you want to use vim for your second brain, but perhaps you aren’t aware of all the advantages of using this ancient text editor as a second brain or PKM. Why use Obsidian as a second brain when you can use vim?

  • Vim is FOSS (free and open source)
  • Vim works with text files – text files will be around longer than you, so your system has longevity
  • Vim is lightweight – when you’re making a quick note, you don’t want to deal with a sluggish, bloated application
  • Plugins – Yes, Obsidian has plugins too, but over the decades, the vim community has built thousands of plugins with all the functionality you could ever want
  • Vim is free (as in beer)
  • Vim keys and commands – Vim is so efficient other second-brain apps like Obsidian have added vim mode into the core application
  • You already use vim

Taking inspiration from other second-brain software, such as Obsidian, there are a few things that you will want to implement in your system:

  1. Links
  2. Sync across devices
  3. Tags
  4. Folders
  5. Markdown
  6. Search
  7. Browse by filename
  8. Backlinks
  9. Mobile Support

Here is what you’ll need

  • vim or neovim
  • vimwiki (a vim plugin)
  • Dmenu (optional)
  • xclip (or some other clipboard manager, also optional)

Install

Vim or Neovim

Vimwiki

Dmenu

  • Dmenu is quick to compile and install. If you’re running Linux (and already have git installed), these three commands will install dmenu.
git clone https://git.suckless.org/dmenu
cd dmenu
sudo make install
  • If you’re running MacOS it may be more challenging to get Dmenu running, though still possible.
  • If you’re running Windows, you may have to look for an alternative or live without the Dmenu functionality.

XClip

Install xclip from your distribution’s repository.

Vimwiki will take care of creating links that are clickable and that take you to the next note. But other second-brain software will give you suggestions or a completion box to find the note you want to link. Let me give you an example.

You’re writing a recipe note for spaghetti bolognese, and you want to link to your homemade pasta recipe. In Obsidian, when you type [[ (the markdown special character for a link,) a box will appear so you can search for the pasta recipe note. Because you can’t create the link without knowing the exact filename.

There are different ways to solve this, you could use netrw within vim, but I found it a little clunky. So instead, I created a short bash script that creates the popup box just like Obsidian does and mapped it to a hotkey.

#!/bin/bash

SEARCHTERM=$(echo "" | dmenu)

CHOSENFILE=$(grep -rnwli PATH/TO/VIMWIKI -e "$SEARCHTERM" | sed 's/\/PATH\/TO\/VIMWIKI\///' | dmenu -l 20)
echo $CHOSENFILE | xclip -selection c

The script opens Dmenu so I can search. The script searches the entire content of the files, so searching “pasta” will give me the option of any note with the word pasta. The sed condenses the path, so it only includes a relative path (relative to your vimwiki home.)

The file path is then saved to the clipboard, and you can paste it into the square brackets. Despite the keypress to open the search menu and then pasting, I find the experience very similar to that of other second-brain software and much better than linking in others (like EverNote.)

Sync across devices

A second brain using vim and vimwiki is just files and directories (folders.) And so you can easily sync them just as you would any other file or folder. Syncthing is a sold solution for syncing your files, and it’s FOSS. Of course, if you already use some other cloud storage, that will work too:

  • Google Drive
  • iCloud
  • DropBox
  • OneDrive
  • NextCloud

Tags

Vimwiki supports tags out of the box; tags are made between two colons.

:tag:

A Tag file must be built in order to use the tags

:VimwikiRebuildTags

If you implement the search function utilizing Dmenu above, however, these tags become obsolete. You can instead create your own tags in any way you like, e.g., @tag or %%tag. Then simply search for either a tag “@tag” or just your tag symbol “@” to get a list of all files containing tags.

You can easily make a script to show you all the tags used or maintain a list of them. I don’t use tags much, so this is not an issue for me.

Folders

If you’re used to note-taking applications with a more traditional hierarchy like EverNote, you can still organize your notes in folders using vim and vimwiki. However, creating links may be more tricky as you will need to pay more attention to the files’ paths.

Markdown

To use markdown instead of vimwiki syntax, set this in your vimrc:

let g:vimwiki_list = [{'path': 'PATH/TO/VIMWIKI/DIRECTORY',
                      \ 'syntax': 'markdown', 'ext': '.md'}]

When your second brain gets big with thousands of notes, you don’t want to use a file explorer to navigate it to edit or open files. Instead, search is much more efficient.

A search function that can search not only the filenames but also the titles and content of your notes is much more helpful. After three years, no one remembers what they called a note, but they can remember it contained an anecdote about juggling. That’s how peoples’ minds work, so that’s how your system must work.

Luckily we already created a script that searches our notes and their contents using Dmenu. So, with a quick change, we can use it to open the note instead of copying it to the clipboard.

Change “st” to whatever terminal emulator you use & bind this to another key.

#!/bin/bash

SEARCHTERM=$(echo "" | dmenu)

CHOSENFILE=$(grep -rnli /PATH/TO/VIMWIKI -e "$SEARCHTERM" | sed 's/\/PATH\/TO\/VIMWIKI\///' | dmenu -l 20)
st nvim /PATH/TO/VIMWIKI/"$CHOSENFILE"

Browse by Filename

Browsing by filename is simple. Either use your default file browser or netrw in vim. I like nnn because it’s fast and vim-like.

Backlinks are the opposite of links. They show you which notes link to the note currently open. Suppose I open my “homemade pasta recipe,” for example. It may contain no links to other notes, but other notes have linked to it. These are called backlinks.

We can use the existing search script to implement backlinks in vimwiki. Simply open the Dmenu search with your hotkey and type the filename of your current file, e.g., “homemade pasta recipe.” The search results will be all the notes with that exact phrase, i.e., the notes that have linked to it.

If your current note’s filename is a common word like “books,” searching for it may return many unrelated results. A simple fix is to search with the .md extension, i.e., “books.md” Which will return only the notes with the exact filename.

Mobile

In order to use your second brain on your mobile device, all you need is a Markdown editor. I’m using Markor. However, any markdown editor with a markdown view mode will work. You need to point the application to your second brain folder on your device.

Markor has the added benefit of a view-only mode. So you can browse your second brain quickly using the links.

Create Your Perfect Second Brain with Vim

The best thing about free and open-source software is that everyone can make it work just the way they want. You don’t have to use my ideas for building your second brain in vim, but I hope they help you find what you’re looking for.

All that’s left is to start using your new second brain!