Skip to content
Oday Bakkour
Back to Knowledge Hub

Useful zsh Aliases

Oday Bakkour profile photo
Oday Bakkour
6 min read
Share
Useful zsh Aliases

Useful zsh Aliases

Zsh (Z Shell) is a powerful and customizable shell that can significantly improve your command-line experience. One of the most effective ways to enhance your workflow in zsh is by using aliases. Aliases are shortcuts that allow you to execute longer commands with shorter, more memorable names.

What are zsh Aliases?

Aliases are essentially abbreviations for commands. Instead of typing a long and complex command every time, you can define an alias that represents that command. This saves time and reduces the chance of errors.

How to Define zsh Aliases

Aliases are typically defined in your .zshrc file, which is located in your home directory. To define an alias, use the following syntax:

zsh.txt
alias alias_name='command_to_execute'

After adding an alias to your .zshrc file, you need to reload the configuration for the changes to take effect. You can do this by running:

zsh.txt
source ~/.zshrc

Examples of Useful zsh Aliases

Here are some examples of useful zsh aliases that can help you improve your productivity:

  • Navigation:
  • alias ..='cd ..' - Quickly move up one directory.
  • alias ...='cd ../..' - Move up two directories.
  • alias ....='cd ../../..' - Move up three directories.
  • alias dl='cd ~/Downloads' - Quickly navigate to the Downloads directory.
  • Git:
  • alias gs='git status' - Check the status of your git repository.
  • alias ga='git add' - Add changes to the staging area.
  • alias gc='git commit -m' - Commit changes with a message.
  • alias gb='git branch' - List all branches.
  • alias co='git checkout' - Checkout a branch
  • alias pull='git pull origin main' - Pull changes
  • alias push='git push origin main' - Push changes
  • System:
  • alias cls='clear' - Clear the terminal screen.
  • alias la='ls -la' - List all files and directories, including hidden ones, in a detailed format.
  • alias h='history' - Show command history.
  • alias ports='netstat -tulanp' - Show all open ports.
  • Docker:
  • alias dps='docker ps' - List running containers.
  • alias dpsa='docker ps -a' - List all containers.
  • alias dim='docker images' - List docker images.
  • Node.js:
  • alias serve='python -m http.server' - Run a simple web server (Python).

Conclusion

Zsh aliases are a powerful tool for customizing your command-line environment and improving your productivity. By defining aliases for frequently used commands, you can save time and reduce errors. Experiment with different aliases to find what works best for you and make your command-line experience more efficient.

More git aliases

These build on the git aliases above. I avoid hard-coding main in push and pull aliases, because not every repo uses that branch name.

  • gpu pushes the current branch and sets its upstream in one step.
  • gundo removes the last commit but keeps its changes staged, so nothing is lost.
zsh.txt
alias gd='git diff'
alias gds='git diff --staged'
alias gl='git log --oneline --graph --decorate'
alias gla='git log --oneline --graph --decorate --all'
alias gsw='git switch'
alias gswc='git switch -c'
alias gpu='git push -u origin HEAD'
alias gundo='git reset --soft HEAD~1'
alias gamend='git commit --amend --no-edit'

Docker and Docker Compose aliases

Use dex mycontainer sh to get a shell inside a container. docker system prune asks for confirmation before deleting stopped containers, unused networks, dangling images and build cache, which is why I do not add -f to it.

zsh.txt
alias dc='docker compose'
alias dcu='docker compose up -d'
alias dcd='docker compose down'
alias dcl='docker compose logs -f'
alias dcr='docker compose restart'
alias dex='docker exec -it'
alias dprune='docker system prune'
  • - jumps back to the previous directory. The -- tells alias that - is a name, not an option.
  • path prints each directory in your PATH on its own line, using zsh's $path array. Single quotes matter here: they delay expansion until you run the alias.
zsh.txt
alias -- -='cd -'
alias proj='cd ~/Projects'
alias md='mkdir -p'
alias path='print -l $path'

Safety aliases

These make destructive commands ask before overwriting or deleting. The -i flag works with GNU and macOS versions of these tools.

  • When you really want the original command, bypass the alias with a backslash: \rm file.txt. The zsh manual documents that quoting any part of the word skips alias expansion.
  • Do not rely on these in scripts. Aliases are meant for interactive use, and a script should say exactly what it does.
zsh.txt
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

Fixing the ports and serve examples

Two of the aliases above depend on your system. netstat -tulanp uses Linux flags and fails on macOS, and many systems no longer have a python command, only python3. These versions work:

zsh.txt
# macOS: list listening TCP ports with the owning process
alias ports='lsof -iTCP -sTCP:LISTEN -n -P'
# Linux alternative:
# alias ports='ss -tulpn'

alias serve='python3 -m http.server 8000'

Global and suffix aliases

zsh has two alias types bash does not. A global alias (-g) expands anywhere on the line, not only at the start. A suffix alias (-s) opens files by extension: type a file name and zsh runs the command you chose.

  • Keep global aliases to short uppercase names so they do not collide with real arguments.
zsh.txt
alias -g G='| grep'
alias -g L='| less'
# usage: ps aux G node

alias -s md=code
alias -s json=code
# usage: typing README.md runs: code README.md

When to use a function instead

An alias is plain text substitution. It cannot put an argument in the middle of a command or run several steps with logic. For that, write a function in the same file:

  • My rule: if it needs $1, more than one command, or an if, make it a function.
  • Do not give a function the same name as an existing alias. Aliases expand first and can break the definition.
zsh.txt
# make a directory and cd into it
mkcd() {
  mkdir -p -- "$1" && cd -- "$1"
}

# commit everything with a message
gcam() {
  git add -A && git commit -m "$*"
}

Organising aliases in ~/.zshrc

Once you have more than a handful, move them to their own file and load it from ~/.zshrc:

  • Group the file with comment headers (git, docker, navigation, safety) so it stays readable.
  • Run alias to list every alias, alias gs to see one, or type gs to check whether a name is an alias, function or command.
  • alias -L prints all aliases in a format you can paste into a startup file, which is handy for moving them to a new machine.
  • Remove one for the current session with unalias gs.
  • Keep the file in a dotfiles git repo so your setup survives a laptop change.
zsh.txt
# in ~/.zshrc
[[ -f ~/.zsh_aliases ]] && source ~/.zsh_aliases

oh-my-zsh alias plugins

If you use oh-my-zsh, you may not need to write many of these yourself. Enable plugins in the plugins=(...) line of ~/.zshrc:

  • git: adds aliases like gst (git status), gco (git checkout), gcb (git checkout -b), gaa (git add --all), gcmsg (git commit --message), glog and gpsup.
  • docker-compose: adds dco, dcup, dcupd, dcdn, dcl and more, using docker compose or the older docker-compose, whichever is installed.
  • aliases: gives you als, which lists every alias by group, and als <keyword> to search. It needs Python installed.
  • Plugin aliases can clash with yours. If two define the same name, the one loaded last wins, so define personal aliases after oh-my-zsh is sourced.
zsh.txt
plugins=(git docker-compose aliases)

Comments

Share your thoughts and join the conversation

Leave a Comment

Loading comments...
RELATED