When naming files, I often use the current ISO formatted date at the beginning. For example, if today is May 30, 2021, I would name a file something like: 2021-05-30_my_file.txt.

Having the date in an ISO format like this means that the files can be easily sorted by time. This works because the date format goes from largest time interval (year) to smallest (day) from left to right.

The only issue with this is that it is often annoying to type this in manually.

I have solved this issue in different ways in the past. I’ve used custom keyboard macros from specialty keyboards, I’ve used AutoHotkey, but I wanted something more native, especially when the in the shell, which I most often am.

Fortunately, for the fish shell, I can easily bind the insertion of any text to a keyboard shortcut.

The way this is done is with the commandline built-in command. This command allows you to modify the current command line buffer contents however you please.

The way this is often used is within a fish function, one that you bind to a keystroke. For this example, what I want to do is insert the output of the command:

date '+%Y-%m-%d'

to my current cursor position.

All this takes is four lines of code.

function add_date
    commandline -i (date '+%Y-%m-%d')
end

bind \ed add_date

The bind \ed portion is binding this to ALT-d. Every terminal is its own little snowflake and so if you are binding a command to something in fish using the CTRL, ALT, WINDOWS, CMD, META, etc. key, I encourage you to use the command fish_key_reader to check what key sequence your shell is actually receiving from your terminal emulator.

I’m sure there are some analogous methods to do something similar in other shells like bash, but fish makes it quite simple.