Generate permutation shell aliases

4 minute read

Diggin’ Dotfiles Collection

It’s time to dig the dotfiles, and to dig in to them! This post is a part of a blog post collection called Diggin’ Dotfiles where I unearth some gems from my personal dotfiles repo erikw/dotfiles.

The following articles are a part of this series:

How I not learned to spell and learned to love generated aliases

How many times per day don’t you mistype a shell command? In focused coding session with the adrenaline pumping as a new feature is closing in on working, you just need to compile the updated code quickly, issuing the make command. Your fingers knows the letters, and presses them all at the same time in excitement. Your result:

$ meak
zsh: command not found
$ maek
zsh: command not found
$ keam
zsh: command not found
$ eamk
zsh: command not found
$ emka
zsh: command not found
$ aekm
zsh: command not found
$ amek
zsh: command not found
$ mkae
zsh: command not found
$ kaem
zsh: command not found
$ kmae
zsh: command not found
$ kame
zsh: command not found
$ eakm
zsh: command not found
$ emak
zsh: command not found
$ meka
zsh: command not found
$ ekam
zsh: command not found
$ mkea
zsh: command not found
$ kema
zsh: command not found
$ akem
zsh: command not found
$ aemk
zsh: command not found
$ akme
zsh: command not found
$ ekma
zsh: command not found
$ amke
zsh: command not found
$ kmea
zsh: command not found
$ make
....

How hard can it be to get the 4 letters right? Sometimes surprisingly hard. Dare I remind you of gti commit, or maybe tig commit?

Given that there are n! permutations you can have a lot of fun with longer commands.

Solution: generated aliases

Let’s simply generate an alias for each permutation of the command you often misspell. Using permute_aliases.sh from my dotfiles we can generate the aliases like this:

$ permute_aliases.sh make
alias keam='make'
alias eamk='make'
alias emka='make'
alias aekm='make'
alias amek='make'
alias mkae='make'
alias kaem='make'
alias kmae='make'
alias kame='make'
alias eakm='make'
alias emak='make'
alias meka='make'
alias ekam='make'
alias mkea='make'
alias kema='make'
alias akem='make'
alias aemk='make'
alias akme='make'
alias ekma='make'
alias amke='make'
alias kmea='make'
alias meak='make'
alias maek='make'

To make use of this, simply store it in a file and let your shell source it on startup:

$ permute_aliases.sh make > ~/.aliases_make
$ echo '. $HOME/.aliases_make' >> ~/.profile  # or whatever your shell loads (like `~/.bashrc` or `~/.zshrc`).

and off you go, to maek no more mistyping.

Tags:

Categories:

Updated:

Leave a comment

Your email address will not be published. Required fields are marked *

Loading...