Typing takes time. Typing without typos is an art. Fixing mistakes in a terminal command is a pain in the neck. Luckily there’s such a thing as aliases. An alias lets you set a keyword and a command it should expand to. For example, I have set a super simple one, which is widely used: alias ..='cd ..'. This allows me to write .. in terminal and it will act like cd .., saving me typing a c, a d and a space, followed by two dots. This seems minor, but when you imagine how often I need to go up a directory in terminal, it’s a huge timesaver.

It’s not just saving time. Some commands I simply cannot remember. So for using rsync to move a file over to or from my server, the command I use is rsync -a --no-owner --partial --info=progress2 -e ssh. There’s no way I’ll remember that, so I aliased it. There’s one snag when it comes to aliases: they can only be used at the start of a line. This rsync command is usually followed by either a local directory or a location on my server. This means typing out either the local address or the username and IP of the server followed by the remote location. This is a somewhat long string which is always the same. This is a hassle and would be so much easier if it could be replaced by an alias. Enter zsh-abbr.

This nifty program lets you set up “abbreviations”, which basically work like aliases, except you can add a flag and they work anywhere on the line! So instead of typing out [email protected]:/ I can now type srv and it automatically (and interactively) expands to that! The automatic expansion is also an improvement over aliases, as it shows the actual command or string in history, not the alias used. I moved over the rsync command to an abbreviation, which means I can now type:

1
copy . srv

Instead of:

1
rsync -a --no-owner --partial --info=progress2 -e ssh . [email protected]:/

And remember, it’s interactive, meaning that after you typed copy and pressed space, it visibly changes to the rsync command. Same for the srv abbreviation, meaning you see the entire command before you press enter!

Adding Abbreviations

First install the program. Then add an abbreviation in terminal:

1
abbr copy="rsync -a --no-owner --partial --info=progress2 -e ssh"

Or, when you want to have it work globally (anywhere on the line), add the -g flag:

1
abbr -g srv="[email protected]:/"

For more commands, see the documentation.

Caveats

This only works in Zsh. If you use Fish, it’s built-in. Bash unfortunately has no support for this as far as I’m aware.