Zsh Suffix Aliases: Let Filenames Run Their Own Commands
Zsh suffix aliases let a filename like report.pdf launch its own viewer. Here's how alias -s works, a verifiable setup, and the simple-command limitation to plan around.
09 Jan 2026, 13:30 UTC

The ten extra keystrokes you type all day
Open a terminal and count how often you type the program name before the file: evince report.pdf, code app.py, mpv clip.mp4. The file's extension already tells the shell everything it needs to know, yet you keep spelling out the launcher. Zsh has a small, declarative answer to this: suffix aliases. Define one line per file type in your ~/.zshrc, and from then on typing the filename alone opens it in the right program.
The thesis here is simple: suffix aliases are the right tool when you want a lightweight, per-file-type shortcut and you don't want wrapper scripts, PATH hacks, or a pile of functions. They are the wrong tool when you need arguments, pipelines, or portability.
What a suffix alias actually does
A regular alias replaces the command word. A suffix alias works differently: when Zsh sees a simple command (a single word, no arguments, no pipes, no redirections) that ends in a registered suffix, it rewrites the line to <command> <filename> before executing it.
You define one with the -s flag:
alias -s pdf='evince'
alias -s txt='gedit'
alias -s mp4='mpv'Add these to ~/.zshrc (plain text editing, no special permissions needed), then reload with source ~/.zshrc or open a new terminal. Now typing report.pdf and pressing Enter launches Evince with that file. The filename itself becomes the command.
A worked example you can verify
Suppose you read a lot of PDFs. The setup is one line:
# In ~/.zshrc
alias -s pdf='evince'After reloading, run these checks in your interactive shell:
- Confirm the alias loaded: run
alias -s. You should seepdf=evince(along with any others you defined). If it's missing, the line didn't get sourced — check for typos or a syntax error earlier in the file. - Test the behavior: in a directory containing a PDF, type its name, e.g.
report.pdf, and press Enter. Evince should open the document. - Confirm the limits: try
report.pdf | wc -correport.pdf --some-flag. The alias will not fire, because these are no longer simple commands — Zsh will report something likecommand not found: report.pdf. That's expected, not a bug.
To remove a suffix alias, run unalias -s pdf and delete the line from ~/.zshrc. Retest: the filename is once again treated as a plain command word.
The trade-offs worth knowing before you commit
Simple commands only. The moment you add an argument, pipe, or redirect, the suffix alias is skipped. If your real workflow is evince --fullscreen report.pdf, a suffix alias won't help — write a function instead.
It can shadow real executables. Zsh checks suffix aliases for any matching word. If you ever have an actual executable in $PATH whose name ends in a registered suffix (say, a script called deploy.sh while sh is aliased), the alias wins and confusion follows. A quick sanity check when something behaves oddly: alias -s to see what's registered, and whence -a somename.sh to see what would actually run.
Zsh only. Suffix aliases don't exist in Bash or POSIX sh. That's fine for your interactive ~/.zshrc, but never rely on them in scripts — scripts should always invoke the real program explicitly anyway.
Where to go from here
Start small: pick the two or three file types you open most often, add one alias -s line each to ~/.zshrc, reload, and test with real files. Verify with alias -s that your definitions survived, and keep unalias -s <suffix> in your back pocket for the day an alias collides with a real command. If you outgrow the simple-command limitation, graduate that file type to a shell function — the alias was never meant to be more than a shortcut.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.