Automating My Journal Entry Numbers in Obsidian with Keyboard Maestro

colorful toothed wheels
Photo by Digital Buggu on Pexels.com

Not long ago I wrote about how I went back to keeping my journal in Obsidian. I was going to try this experiment for all of 2022 to see how it worked out. I’ve flip-flopped on this over the last year or so, but you can check out the recent post as to why I made this decision.

Since I began this particular incarnation of my journal, back in 2017, I began giving each entry a unique entry number. I took this idea from Isaac Asimov, who used a similar method to simplify the indexing his of book Asimov’s Biographical Encyclopedia of Science and Technology. The journal spans multiple volumes (9 large Moleskine books and now, one file) and the entry numbers allow me to index the journal without worrying about which volume or page number the entry is on.

I am always looking for small efficiencies in workflow, and as I have been keeping my journal in Obsidian, I wondered if there was an easy way to automate entering the next entry number heading. It’s a small thing, but given that the journal is a text file, and each entry number a level 2 heading, it should be do-able. Here is how I managed to automate this:

Obtaining the next entry number

First, I wrote a command to search my journal file for all level 2 headings (that is, heading that begin with ## in Obsidian) and are followed by a numeric sequence. A typical heading entry looks like this:

## 2345

It took about five minutes of messing around with some Unix commands to do the trick. Here is what I came up with:

egrep "^##\s(\d+)" ~/Documents/DFC/Writing/Journal/2022\ Journal.md | tail -1 | sed 's/## //'

Running this command returns the number part of the last level 2 header in the journal file. For instance, when I run it right now, it returns: 2155

Here is how it works:

  • egrep "^##\s(\d+)": searches for any lines in the file that begin with ## followed by a space and then a sequence of one or more digits. Since ## 2155 matches this pattern, any lines in this format will be returned.
  • ~/Documents/DFC/Writing/Journal/2022\ Journal.md: the name of the file to search. That is, my current journal file.
  • | tail -1: take the output of the egrep command about, which will be a list of all the heading level 2 entry numbers in the file (like ## 2155) and filter it through the tail -1 command, which returns the last item in the list.
  • | sed 's/## //': take the last heading that comes from the previous command and filter it through the sed command to strip out everything but the number itself. What I am left with after this is just the entry number.

Getting the entry number into Obsidian

I use Keyboard Maestro for a lot of text expansion and miscellaneous automation. I decided I could use it here to get the entry number into Obsidian. I created a Keyboard Maestro macro called “New Journal Entry” that is triggered whenever I type ;;dd. (Note, this doesn’t apply to just Obsidian, it will do it when I type that sequence of keys anywhere.) The following macro is run when I type that key combination:

Here is how it works:

  • first, it executes the Unix command discussed above to obtain the next entry number from the file, and stores the result in a variable called CurEntry. If I ran this right now, the value of CurEntry would be “2155”.
  • next, it increments the value of the CurEntry by 1, making it 2156.
  • finally, it prepends the number with ## and inserts the value at the position of the cursor in the document. Keyboard Maestro automatically handles replacing the triggering text (;;dd) with the inserted value.

Here is what it looks like in action:

This might seem like a lot of effort to type out a number, but keep in mind, it took less time for me to create the automation than it did to write this post. Also, I’ve already got more than 2,100 entries in my journal and each time, I find myself having to check the previous entry number before entering the new one, and occassionally, I make a mistake, which is a nuisance to correct. This little macro eliminates all of that.

Perhaps even more important, it is one less thing I have to think about. Instead of sitting down to journal and first having to figure out the entry number, now I can just start writing.

There are probably other tools that could be used to achieve the same results. I’ve just happened to be a Keyboard Mastro user for a long time and have a cache of automations that I’ve created over the years stored there, so it seemed the logical place for this one.

Written on February 13, 2022.

Did you enjoy this post?
If so, consider subscribing to the blog using the form below or clicking on the button below to follow the blog. And consider telling a friend about it. Already a reader or subscriber to the blog? Thanks for reading!

Follow Jamie Todd Rubin on WordPress.com

Comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.