Journaling on the Command Line

Journaling on the Command Line

I'm not the first person to write a script for journaling from the command line, and I won't be the last. This is just a short blog post detailing the steps I took setting it up, and demonstrating how it works!

I got my inspiration from this comment on Hacker News. I decided it'd be a good way to get me into writing a couple of words a day. But, because I spend my time split across four different personal operating systems, I needed a way to sync them across each other - so I started looking for solutions. I know DropBox has a few command line utilities, but I use Google Drive much more than I use DropBox, so I decided ideally I'd find a script-able GDrive tool I could use. I found Drive which can do everything I need it to!

The first step was setting up Drive, which requires Go. I configured Go following the instructions after downloading and setup my GOPATH and PATH environment variables. I installed drive with a simple command from it's documentation:

$ go get github.com/odeke-em/drive/cmd/drive

(If you've never used Go before, this takes a while and gives no output.)

The next step is configuring drive and logging in, which is pretty easy. Run these commands and follow the given prompts.

$ mkdir ~/gdrive
$ drive init ~/gdrive

The last step in my setup is adding a quick function to your ~/.bashrc or other shell configuration dotfile. My script creates a folder for the year inside of the Journal directory, and then names each file in the format mm-dd.txt. It loads all files from GDrive before opening today's file, and then writes changes back to GDrive when the editor is closed. This is my configuration, appended to the end of my ~/.bashrc:

# Define editor if undefined
EDITOR=vim
journal() {
  drive pull -no-prompt ~/gdrive/Journal
  mkdir -p ~/gdrive/Journal/`date +%Y`
  $EDITOR ~/gdrive/Journal/`date +%Y`/`date +%m-%d`.txt
  drive push -no-prompt ~/gdrive/Journal
}

After a quick reload of your ~/.bashrc file via $ source ~/.bashrc, you can now run the journal command to open a new text file inside a synced directory, named after today's date! Written entries are now accessible via Google Drive or on your filesystem.