Artboard 16light, inspiration, solution, idea, innovation,Google Sheets iconSwift icon
Published at
Updated at
Reading time
2min
This post is part of my Today I learned series in which I share all my web development learnings.

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.html on your terminal will be expanded and shuffled to cat index.html. ๐Ÿ˜ฒ

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.

Example showing a the execution of the file "sort-by" which results in the display of its content.

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"

Running a SSH link on the terminal which results in cloning of this repo

Do you know of more suffix alias use cases? If so, I'd love to hear them via email or on Twitter!

Related Topics

Related Articles