I recently came across one of Fish shell best and likely most underrated features, abbreviations ; a great alternative to aliases and dare I say a full replacement.
The Problem with Aliases
An alias is a (usually short) name that the shell translates into another (usually longer) name or command. Aliases allow you to define new commands by substituting a string for the first token of a simple command.
The main issue is that aliases are expanded behind the scenes, take the following alias:
alias rm='rm -Rfi'
The way aliases work on most shells the following drawbacks become apparent:
- They hide what is really happening as they are resolved behind the scenes
- Makes copy-pasting a command-line for instructions to others, difficult of not impossible
- Command history will be recorded as the alias, so history loses value
- Aliases become less valuable if you have to edit the options
The better approach
Abbreviations work on the same principle as aliases but with the main advantage that an abbreviation will get expanded βliveβ as is being typed. Letβs look at the following example:
abbr --add miex "iex --erl "-kernel shell_history enabled" -S mix"
I converted this from an alias that I used on a daily basis for elixir development, here are the advantages so far:
- The
abbr
expands βliveβ, the git completions work as normal and commandline doesnβt have to lie or do any other hacks like that. - Clean history. Using abbr means other developers can understand your terminal history.
- Easy to use a shortcut thatβs close to what you want and edit it.
Summary
-
abbr
make for a better and more understandablealias
- Iβm replacing all my aliases with abbreviations
Top comments (0)