Dates Are Easy

automatization — not so much

It’s hard to believe it, but computers can do what users tell them, instead of the other way around.

Tim Marinin

Got an enlightenment yesterday1 night.

Up until yesterday, I put the date into the files for this website by hand. That’s actually not hard, but not comfortable either, and the more I did it, the less comfortable it felt. Like a pebble in a shoe.

The source for pages in Hugo is Markdown files that have meta-information added in the form of a TOML (or YAML, or even JSON, whatever you like) header called “Front Matter” in Hugo terms. For instance, here’s how the file with this very post begins:

title = "Dates Are Easy"
subtitle = "automatization — not so much"
date = 2020-10-16T12:55:25+03:00
tags = ["linux", "hugo", "school didn't teach me this"]
categories = ["IT"]
> It's hard to believe it, but computers can[...]

As you can see, the date is an ISO 8601 string. Composing that string by hand every time is not that much fun. I need to have the current date (because the current date is what I’m usually looking for here) inserted automatically. But how?

Simply taking a Git commit date (since it’s all stored in Git anyway) is not a good solution: the date would change every time I fix some typos of formatting.2

Hugo has a builtin solution in the form of file templates. It can be arranged so that a command like

$ hugo new /posts/2020/

creates a file with the current date already pre-populated in the Front Matter (as well as some other fields, if required). This suits fine for short notes, but a post, even if it isn’t too long, can take quite some time to write,3 and I want it to have the date (and time) of when it was finished, not of when the first idea appeared and the file got created.

I even started contemplating using a Vim plugin that can paste the date in the required format, between other things. Unfortunately, I don’t feel comfortable composing posts in Vim4, and ReText that I am currently writing this post in has no plugins.

It finally dawned on me yesterday:

What exactly prevents me from inserting the current date by means of the system?!

Isn’t it Linux that I’m using? I’ve been told that in Linux one can do anything — and even more. Can I not make it paste the current date in ISO 8601 format wherever I please? I can, actually!

Any decent system interface allows binding an arbitrary command to a keyboard shortcut, and so does KDE that I use. Let’s say it’s Ctrl-Alt-D. I only need to figure out the command to paste the current date. And to paste where, by the way?

I failed to promptly find a command to paste some arbitrary text to the cursor position in any window. Instead, I found the xsel utility that works with the system clipboard and can put the current date there. Let’s give it a try:

date -Is | tr -d "\n" | xsel -b -i

It works!5 The current date appears in the clipboard, formatted appropriately, and then a ‘Ctrl-V’ (or a ‘Shift-Ins’, whichever you like more) can be used to paste the date anywhere. But it takes two keyboard combinations, and the clipboard gets overwritten — somewhat messy. I’d like it to be one shortcut, and keeping the clipboard intact…

Google tells me there’s an xdotool that can do the typing for me. Can I make it work with a keyboard shortcut? Let’s try to bind something like

xdotool type "test"

to Ctrl-Shift-T and give it a try in a text editor… Oops! A “save as” dialog opened up. What the hell?!

It turns out, xdotool does type, but the keyboard input is still processed, and I have Ctrl and Shift pressed! So the text editor (it was Kate on the occasion) didn’t get a Ctrl-Shift-T (that one was caught by the system) but received Ctrl-Shift-E (that had no meaning), then Ctrl-Shift-S (“save as”), and then Ctrl-Shift-T again, which was ignored because the “save as” dialog was already opened.

That won’t do! Fortunately, xdotool has the --clearmodifiers option that “releases” all the keys (including CapsLock) before typing and re-presses them again after the job is done. Trying this…

Much better. xdotool also “released” the T, so the first letter did not appear, and afterwards “re-pressed” Ctrl and Shift (which made the text editor’s behavior very weird until I pressed this combination again), but that’s not a big deal: my shortcut uses D that never appears in the date string, and I can ask xdotool to “release” the keys it “re-pressed” again:

xdotool type --clearmodifiers $(date -Is | tr -d "\n") && xdotool keyup ctrl alt d

Pressing Ctrl-Alt-D

There we go! It works everywhere, be it the console, or ReText, or even Vim…

Of course, figuring it all (especially xdotool) out took time6. Of course, someone may call it all “unnecessary geekery”. But my computer now does what I want it to do the way I want. Which is actually the reason to own a computer in the first place…

I think these things need to be taught at school.

  1. This post was originally written in Russian on June 6, 2020. ↩︎

  2. It is actually useful (and used on this site) to show the date of the last update. For example, that post was written in July last year; a month ago I corrected some errors that appeared in the process of moving the site from Known to Hugo, so the header says July 2019 (that’s taken from the Front Matter), and the date below the text (just over the tags) is May 2020 (the date of the latest Git commit). ↩︎

  3. Some things I write in short bursts when I happen to have spare time, some things I need to think over for a period of time. I could compose a post in a draft file and copy it into a new file just before publication, but that’s too much hassle… ↩︎

  4. I wrote a separate post about this. I use Vim with pleasure for config editing, bash scripting, and Go coding, but I feel extremely uncomfortable using it to write a coherent text in Human. I can do a changelog, but a readme is already a torment. ↩︎

  5. date -Is produces the date properly formatted, tr -d "\n" rids the resulting string of the carriage return symbol (the imaginary «Enter» that date puts in there for some unknown reason), and xsel -b -i puts the resulting string into the system clipboard. ↩︎

  6. As always. ↩︎


Comments can be sent as webmentions or by email.