Aliases and Abbreviations

So, going back to my love of all things text and, by extension, terminal-based. I discovered a feature in the Fish Shell that not only helps you build shortcuts, but also can help you with longer commands or parts of commands. That is abbreviations. At first glance, this built-in looks just like the more traditional alias tools in lots of other shells out there. You type one series of characters, like ,gc for git commit or ,d.o for ssh degruchy.org (or if you’ve got an SSH alias, ,d.o to ssh d.o – now thats layers). This is extremely handy when having to do tedious or just repetitive commands that may not be in the immediate history, or just long.

abbr Extends alias

Well, not directly, anyway. Fish shell’s abbr command is more akin to a text expansion (like in Espanso). When you type the trigger (and press space/return) that text is expanded to the full form. Meaning if you type ,fse<space> in my shell, you’d get fossil status --extra expanded out for you. It’s also useful just in the alias sense, because typing ,fse<return> gets you the command run, but also echoing the expanded command above it.

But wait… there’s more!

By default, these expansions happen when you type the trigger at the beginning of a command. That’s fine for most alias-esque usage, but you can also tell Fish to expand them anywhere.

abbr -a L --position anywhere --set-cursor "% | less"

Or even have them activate as part of a specific command:

With --command COMMAND, the abbreviation will only expand when it is used as an argument to the given COMMAND. Multiple --command can be used together, and the abbreviation will expand for each. An empty COMMAND means it will expand only when there is no command. --command implies --position anywhere and disallows --position command. Even with different COMMANDS, the NAME of the abbreviation needs to be unique. Consider using --regex if you want to expand the same word differently for multiple commands.

I really love the idea of abbr’s. They both help shorten typed commands, but also help you remember or expand arguments and other parts to speed up your day and reenforce learning by expanding the shortcut and not just replacing it.

Respond via email.