Scripts for Johnny Decimal

by Rosa Richter

For a few years now, I’ve been using the Johnny Decimal system to keep all my “stuff” organized. It’s a great system because it transcends a single medium: files in both meatspace and cyberspace can live within a single shallow hierarchy, and every topic gets a short numeric ID that can be easily memorized if you return to that topic frequently. Please check out their website for more information on the system itself if you’re unfamiliar; From here on out I’m going to assume you understand the system, since you’re reading this post about scripts I’ve written for it.

jd: the navigation command

jd lets you search directories, and when you select one and press ENTER, it cds to that directory. It can be used outside of a Johnny Decimal folder structure, too! Substitute ${JD_HOME} for the path to your own Johnny Decimal system.

jd() {
  dir=$(fd --type dir . "${JD_HOME}" | fzf --style full --preview 'eza --tree --color=always {}')
  cd "${dir}"
}

This uses fd to find all directories in your Johnny Decimal system, and pipes that into fzf so you can fuzzy-search through them and select one. The --preview argument here uses eza to preview the contents of that directory as a tree. Once you’ve selected a directory, the function cds you to it.

This needs to be defined as a function in your shell because it changes your current directory. Add the code above to your shell’s startup configuration (.bashrc, .zshrc, etc.).

jde: the edit command

jde opens your $EDITOR to any text file in your Johnny Decimal system. It uses fzf similarly to the jd command above, but this time you’re searching through text files. It even gives you a preview of the file while you’re searching.

#!/usr/bin/env bash
set -euo pipefail

file=$(rg --files-with-matches --type=all --no-binary . "${JD_HOME}" | fzf --style full --preview='bat --color=always {}')

${VISUAL:-$EDITOR} -- "${file}"

Instead of fd, this script uses ripgrep (rg) because it filters out a lot of file types we want to ignore, like those mentioned in a .gitignore file, hidden files, and binary files. rg prints the paths of files that match, which are then piped into fzf. I am using the --preview option here to print the contents of the file with line numbers and syntax highlighting using bat Once you select the file in fzf, the path is opened in your $EDITOR.

jdo: open any file

#!/usr/bin/env bash
set -euo pipefail

file=$(fd --type file . "${JD_HOME}" | fzf --style full)

xdg-open "${file}"

This one is similar to jde but uses xdg-open instead of your $EDITOR. xdg-open opens a file in the default program for that file.

Comments

Have you responded to this on your own site? Enter the URL of your post to add it to the comments. This site supports Webmentions.

Joe Crawford

Liked this post