Using Vim to record a maintenance event timeline

Vim can do lots of things

After-hours maintenance is something my team and I encounter frequently. Whether it's customer turn-ups, circuit migrations, or firmware upgrades—we get used to the 10pm to early morning working hours!

Often, we like to record a rough timeline of events during the maintenance period in order to have timestamped context for re-tracing our steps and/or investigating persistent issues before/during/after the maintenance period. Historically I've manually inserted timestamps as we never keep too granular of a timeline. However, this last go-around I decided to dig a little into Vim and look into how to automate the insertion of timestamps whenever I start a new line in the editor.

Insert mode maps

Vim/vi is nearly limitless in its customization. One of the configurable properties is key mappings. Key mappings allow the creation of a shortcuts for the purpose of repeating a sequence of keys or commands.

In my use case, I want to map the automatic input of the formatted, current time using Vim's internal strftime() function whenever we drop down to a new line in the editor. The mapping below is what I came up with:

:imap <buffer> <CR> <CR><C-R>=strftime("%Y-%m-%d %H:%M:%S :: ")<CR>
  • imap tells Vim that this is an insert mode map
  • <buffer> tells Vim to only consider the mapping in the buffer in which it is defined
  • The first <CR> is the left-hand-side of the map command, which defines the keys used to activate the shortcut. <CR> and <Enter> are synomymous
  • <C-R> tells vim to insert the following expression at the cursor
  • =strftime("%Y-%m-%d %H:%M:%S :: ") is our strftime() function to grab the current date and time in the preferred format
  • The final <CR> executes the expression

If the above is entered as a Vim command, a timestamp will be inserted at the beginning of the line every time Enter is pressed while in insert mode.

Make it a Vim filetype

Now that we have the functionality we want, the next step is to automate the key mapping when it's suitable.

First, enable the filetype plugin in your .vimrc file:

filetype plugin on

Create the following directories:

  • ~/.vim/ftdetect
  • ~/.vim/ftplugin

In my example, I'm going to define a new filetype called toe (timeline of events) with the extension of .toe.

Let's define our filetype detection file:

~/.vim/ftdetect/toe.vim

autocmd BufNewFile,BufRead *.toe set filetype=toe

This sets the Vim filetype to toe when the filename ends in .toe.

Lastly, we define the command(s) we want active when the filetype is toe:

~/.vim/ftplugin/toe.vim

imap <buffer> <CR> <CR><C-R>=strftime("%Y-%m-%d %H:%M:%S :: ")<CR>

Now, whenever you manually set the filetype to toe or open a file that ends in .toe, Vim will know to insert a timestamp at the beginning of each new line.

Originally, I wanted to make a quick CLI tool in Go to replicate this behavior, but I'm glad that I went down the route of learning a bit more about Vim/vi.