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:
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:
source ~/.zshrcExamples 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 branchalias pull='git pull origin main'- Pull changesalias 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.
gpupushes the current branch and sets its upstream in one step.gundoremoves the last commit but keeps its changes staged, so nothing is lost.
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.
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'Navigation aliases
-jumps back to the previous directory. The--tellsaliasthat-is a name, not an option.pathprints each directory in your PATH on its own line, using zsh's$patharray. Single quotes matter here: they delay expansion until you run the alias.
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.
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:
# 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.
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.mdWhen 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 anif, make it a function. - Do not give a function the same name as an existing alias. Aliases expand first and can break the definition.
# 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
aliasto list every alias,alias gsto see one, ortype gsto check whether a name is an alias, function or command. alias -Lprints 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.
# in ~/.zshrc
[[ -f ~/.zsh_aliases ]] && source ~/.zsh_aliasesoh-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),glogandgpsup. - docker-compose: adds
dco,dcup,dcupd,dcdn,dcland more, usingdocker composeor the olderdocker-compose, whichever is installed. - aliases: gives you
als, which lists every alias by group, andals <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.
plugins=(git docker-compose aliases)Comments
Share your thoughts and join the conversation
