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!

Was this TIL post helpful?
Yes? Cool! You might want to check out Web Weekly for more quick learnings. The last edition went out 11 days ago.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles