How I made writing my code journal easier
Tags: journaling, vim, bash, automation
Published: 2020-04-07 08:32:07

Back in September [Jacqui](https://dev.to/jacquibo) wrote about [code journalling](https://dev.to/jacquibo/why-you-should-keep-a-code-journal-code-journaling-pt-1-of-4-k35) and I thought it sounded like a great idea.

So I set up a directory called `Documents\codelog` and created a directory called `2019-09` in it and then started creating a new file each day called `2019-09-27.md` with vim. Then I would read in the template file I had created and add the date to the top line, and finally I would start journaling. And, that doesn't include the time I spent working out what the date was each day.

This soon proved to be a disincentive to journalling, all I wanted to do was note down a quick thought or discovery and I had to go through 5 minutes typing to achieve it. So I decided that I needed to automate the process somehow. 

So `zsh` and `vim` to the rescue. First I created a script called `today` in my codelog directory.

~~~
t=`date +%F`
y=`date +%Y`
m=`date +%m`

mkdir -p $y-$m
vi $y-$m/$t.md
~~~

Here I extract today's date (`$t`), the year (`$y`) and month (`$m`) which is all I need to create the directory for this month (if it's the first day of the month this is necessary) using `mkdir` by using `-p` there is no error if the folder already exists. Then I execute `vi` on the required file for today's log, this will create it if it doesn't already exist and just open it if it does. 

That helps, but I still need to copy in the template file and add today's date again. Fortunately, vim can help me out here in the form of a function in my `.vimrc`. 

~~~
function AddTemplate(tmpl_file)                                                                                         
  let fully_qualified_file = expand('%:p')                                                                              
  if 0 != match(fully_qualified_file, $HOME . '/Documents/codelog/.*/*.md')                                             
    return                                                                                                              
  endif                                                                                                                 
  exe "0read " . a:tmpl_file                                                                                            
  exe "normal Addate  "                                                                                                 
  set nomodified                                                                                                        
endfunction                                                                                                             
                                                                                                                        
autocmd BufNewFile *.md call addTemplate("~/Documents/codelog/template.md")
~~~

This checks if the file is inside my `codelog` folder and if it isn't then it returns straight away, if it is then it reads in the template file and adds `ddate<tab>` to the end of the first line. Through the magic of [UltiSnips](https://github.com/SirVer/ultisnips) `ddate` expands out to today's date. Finally, it sets the file to unmodified so I can just quit if I decide I don't need to write anything just now. 

The `autocmd` line calls this function whenever I create a new `.md` file with the argument of my template file. 

So now when I type `./today' in my code log folder I automatically open a file with the correct name in the right directory with the template copied in and the date added to the title line. Now adding a thought takes next to no time and I find myself journaling much more than before.

One final trick is to make sure that each day's journal gets added to my github archive so that I can check back in the logs from which ever machine I am on. 

~~~
#!/bin/bash

git pull origin 
git add *
git commit -m "nightly update"
git push origin
~~~

I set up a cron job to run this each night on my desktop and I can run it by hand on my laptop as and when it is needed.