Skip to content

Lesson 7 of 9 · 18m

Aliases and Functions: Your Personal CLI

The shortcuts that save thousands of keystrokes a day — aliases for long commands, functions for anything with arguments, and the naming discipline that keeps them maintainable.

You type git status forty times a day. gst is two characters. Multiply across every command you use and aliasing saves an hour a week — but only if the aliases are for commands you actually use.

Aliases: the 80%

alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias ll='ls -lah'
alias ..='cd ..'
alias ...='cd ../..'
alias d='docker'
alias k='kubectl'

The alias is for commands without arguments — alias c='clear' is fine, alias deploy='./deploy.sh' is fine, alias build='docker build -t $1' is wrong — aliases don't take arguments.

Functions: for anything with arguments

mkcd() { mkdir -p "$1" && cd "$1"; }
take() { mkcd "$1"; }     # same thing, nicer name

killport() {
  lsof -ti :"$1" | xargs kill -9
}
# killport 3000 — kills whatever's listening on 3000

Functions can take arguments, loop, branch — everything a full script does. They're the answer when the alias gets complicated.

The naming discipline

  • Short aliases for frequent commands (gs, gp, k).
  • Longer names for rare-but-important commands (deploy-prod, db-reset-staging).
  • type and alias to look up what's defined — alias | grep docker finds what you forgot.

Field notes

  • Add to .bashrc/.zshrc — a comment explaining why it exists ("because the man page is 2000 lines") is the one documentation that pays.
  • abbr in fish or zsh-abbr expands aliases inline so you see the full command before running — the best of both worlds.
  • history | awk '{print $2}' | sort | uniq -c | sort -rn | head -20 — find the commands you actually run and alias those, not the ones you imagine.