Suffix aliases (-s) in Zsh (#tilPost)

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 …


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

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,gitignore,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!


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2022-07-04T22:00:00+00:00) Suffix aliases (-s) in Zsh (#tilPost). Retrieved from https://www.scien.cx/2022/07/04/suffix-aliases-s-in-zsh-tilpost/

MLA
" » Suffix aliases (-s) in Zsh (#tilPost)." Stefan Judis | Sciencx - Monday July 4, 2022, https://www.scien.cx/2022/07/04/suffix-aliases-s-in-zsh-tilpost/
HARVARD
Stefan Judis | Sciencx Monday July 4, 2022 » Suffix aliases (-s) in Zsh (#tilPost)., viewed ,<https://www.scien.cx/2022/07/04/suffix-aliases-s-in-zsh-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Suffix aliases (-s) in Zsh (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/04/suffix-aliases-s-in-zsh-tilpost/
CHICAGO
" » Suffix aliases (-s) in Zsh (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2022/07/04/suffix-aliases-s-in-zsh-tilpost/
IEEE
" » Suffix aliases (-s) in Zsh (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2022/07/04/suffix-aliases-s-in-zsh-tilpost/. [Accessed: ]
rf:citation
» Suffix aliases (-s) in Zsh (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2022/07/04/suffix-aliases-s-in-zsh-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.