Suffix aliases (-s) in Zsh
- Published at
- Updated at
- Reading time
- 2min
I like geeking out on my local shell setup, and my dotfiles hold a few aliases I can't live without โ ll
, ni
, nr
and all these other two-character commands are deeply engrained in my muscle memory.
To define an alias, you set a new command name and map it to another command.
alias ll='exa -la --git --icons'
Today I learned that the alias
command in Zsh also supports a -s
flag which enables suffix aliases. As a result, this is a valid alias in Zsh:
# "Run" the file to look at its content
# $ index.html
# -> cat index.html
alias -s html=cat
You can "just run files" with a suffix alias without defining a command or making it an executable. After defining the alias above, executing index
on your terminal will be expanded and shuffled to cat index
. ๐ฒ
If you want to learn more about suffix aliases, access its manual via man zshbuiltins
on the command line.
But there's more! You can define a suffix alias for multiple file types, too. I added cat
(aliased to bat on my machine) as the standard handling of the following file formats.
# "Run" the file to look at its content
# $ ./readme.md
# -> cat ./readme.md
alias -s {js,json,env,md,html,css,toml}=cat
I'm super into this shorter way of looking into text files quickly.
This functionality is pretty neat already, but there's more (even though it might be a bit of a hack). @_smhmd pointed out that they use suffix aliases to save typing git clone
when cloning a git repository. They paste the repo SSH link into the terminal, and it's expanded to a proper clone
command. Smart! ๐ฒ
# "Run" ssh links to clone repos
# $ git@github.com:stefanjudis/dotfiles.git
# -> git clone git@github.com:stefanjudis/dotfiles.git
alias -s git="git clone"
Do you know of more suffix alias use cases? If so, I'd love to hear them via email or on Twitter!