Zsh’s global and suffix aliases let you open files of specific types with your preferred application, without typing the application’s name every time.
Let’s say you want to open all .txt files with vim and all .md files with bat. Here’s how you’d set that up in your .zshrc:
# Open .txt files with vim
alias -g TXT='vim'
# Open .md files with bat
alias -g MD='bat'
Now, if you type foo.txt and press Enter, Zsh will automatically expand it to vim foo.txt. Similarly, report.md becomes bat report.md.
This works because -g creates a global alias. Global aliases are expanded anywhere in the command line, not just at the beginning. When Zsh sees foo.txt, it recognizes .txt as a suffix associated with the global alias TXT and substitutes vim before executing the command.
To see this in action, you can create a couple of dummy files:
touch my_notes.txt
touch project_plan.md
Then, from your Zsh prompt, simply type:
my_notes.txt
And vim will open, displaying my_notes.txt. If you then type:
project_plan.md
bat will display project_plan.md with syntax highlighting.
The magic here is in Zsh’s suffix alias expansion. When you define alias -g SUFFIX='command', Zsh registers command as the handler for any file ending with . + SUFFIX. It’s not just about the literal string .txt; it’s about the suffix txt being associated with the alias TXT.
You can define as many of these as you need. For instance, opening .json files with jq and .yaml files with yq:
# Open .json files with jq
alias -g JSON='jq .'
# Open .yaml files with yq
alias -g YAML='yq .'
Now, config.json will be processed by jq . config.json, and deployment.yaml will be handled by yq . deployment.yaml.
The real power comes from combining global aliases with other Zsh features. For example, you can use them with find or grep:
# Find all .md files and display them with bat
find . -name "*.md" -exec $MD {} \;
# Search for a string in all .txt files
grep "important keyword" $(find . -name "*.txt")
Here, $MD is expanded to bat before find executes it, and $(find . -name "*.txt") expands to a list of .txt files, which are then passed to grep (though in this specific grep example, grep "important keyword" *.txt would be more direct if you’re in the directory containing the files).
A common pitfall is forgetting the . before the suffix when defining the alias, or trying to use a non-global alias for this purpose. For example, alias TXT='vim' would only expand if you typed TXT directly. The -g flag is crucial for suffix expansion. Also, be mindful of case sensitivity; txt and TXT are distinct suffixes.
The system doesn’t just rely on the suffix; it’s the combination of the suffix and the global alias definition that creates this behavior. Zsh maintains an internal mapping of registered suffixes to their corresponding global alias commands. When you type a filename, it checks if any part of the filename matches a registered suffix. If it finds a match, it performs the substitution.
You can see your currently defined global aliases with alias -g. This will list all your global aliases, including the ones you’ve set for file suffixes.
The next step in mastering Zsh’s command-line magic is exploring how these global aliases interact with Zsh’s completion system, allowing for intelligent filename completion based on your defined aliases.