Plugins, frameworks and aliases

Zsh · Practical · 10 min

Plugins give fish-like suggestions and highlighting, but each one costs startup time.

A zsh plugin is just a script you source from .zshrc, sometimes adding a directory to fpath for completions. Two are near-universal:

  • zsh-autosuggestions: shows a grey suggestion from history as you type; Right arrow or End accepts it.
  • zsh-syntax-highlighting: colours the command line, red for unknown commands. Source it last, after other plugins and key bindings.

You can manage plugins three ways: clone repositories and source them yourself, use a lightweight plugin manager (antidote, zinit, zap), or use a framework. Oh My Zsh is the best-known framework: it bundles hundreds of plugins (plugins=(git docker kubectl)) and themes. It is convenient but loads a lot; measure with time zsh -i -c exit and keep the list short.

Beyond normal aliases, zsh has two extra kinds:

  • global aliases (alias -g G='| grep -i') expand anywhere on the line, not only in command position,
  • suffix aliases (alias -s md=less) open a file by typing its name: README.md runs less README.md.

Use global aliases sparingly: they expand even inside arguments where you did not expect them. alias with no arguments lists everything currently defined, which helps when a framework surprises you.

Example

# Manual setup (no framework): clone once, then source in ~/.zshrc
git clone https://github.com/zsh-users/zsh-autosuggestions ~/.zsh/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting ~/.zsh/zsh-syntax-highlighting
# ~/.zshrc
source ~/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh
alias gs='git status'          # normal alias: command position only
alias -g G='| grep -i'         # global alias: anywhere in the line
alias -s md=less               # suffix alias: README.md -> less README.md
source ~/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh   # last
# At the prompt:
ps aux G zsh                   # runs: ps aux | grep -i zsh
time zsh -i -c exit            # how long your config takes to load
# With Oh My Zsh instead, in ~/.zshrc:  plugins=(git docker kubectl)

Remember

  • Plugins are sourced scripts; source syntax highlighting last
  • alias -g expands anywhere, alias -s opens files by extension
  • Measure startup time and keep plugins few

Watch out for

  • Loading dozens of Oh My Zsh plugins and then living with a one-second shell startup.

Check yourself

  1. After `alias -g L='| less'`, what does `cat log.txt L` run?
  2. Why should zsh-syntax-highlighting be sourced last?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html