Aliases and functions give short names to commands that you run frequently. This article is a collection of practical shortcuts for navigation, files, macOS, Python, Git, and GitHub.
Aliases vs. Functions
Use an alias for a fixed command replacement and a function when the shortcut needs arguments, multiple commands, or conditional logic.
| Alias | Function | |
|---|---|---|
| Best for | A fixed command or fixed options | Arguments or multiple commands |
| Example use | Replace h with cd ~ | Create a directory and enter it |
| Definition | alias h='cd ~' | mkcd() { mkdir -p "$1" && cd "$1"; } |
alias h='cd ~'
mkcd() {
mkdir -p "$1" && builtin cd "$1"
}Use h to go to the home directory and mkcd new-project to create and enter a directory.
These examples are intended for interactive Zsh sessions, not shell scripts. Many simple aliases also work in Bash, but functions and command options can differ between shells and operating systems.
Where to Define Aliases and Functions
For a small configuration, add aliases directly to ~/.zshrc:
alias h='cd ~'Reload the file after editing it, or start a new shell:
source ~/.zshrcFor a larger configuration, keep aliases in purpose-specific files such as $ZDOTDIR/aliases/git.zsh and load them from .zshrc:
[[ -f "$ZDOTDIR/aliases/git.zsh" ]] && source "$ZDOTDIR/aliases/git.zsh"See Organizing Zsh Configuration for a complete example using $ZDOTDIR and a modular .zshrc.
Dependencies and Compatibility
Most examples use standard macOS or Unix commands, but some require additional software:
| Command or feature | Requirement |
|---|---|
gls | GNU core utilities: brew install coreutils |
open, osascript, defaults, pmset | macOS |
code | Visual Studio Code command-line launcher |
imgopt | ImageOptim |
gh | GitHub CLI |
git, curl, zip | Install separately if unavailable |
Copy only the shortcuts that match your tools and workflow. Test each addition in a new terminal before relying on it.
Navigation and Files
Change Directories
cs() { builtin cd "$@" && command ls -A }
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias cb='cd -'
alias d='cd ~/Desktop'
alias dl='cd ~/Downloads'
alias h='cd ~'
alias /='cd /'
# Change to the location shown in the frontmost Finder window.
cdf() {
builtin cd "$(osascript -e 'tell app "Finder" to POSIX path of (insertion location as alias)')"
}The cs function changes directory and then runs ls -A. builtin cd ensures that it always calls Zsh's built-in command. The other aliases provide short names for common destinations.
List Files
alias l='gls --color --group-directories-first -F'
alias la='gls --color --group-directories-first -F -A'
alias ll='gls --color --group-directories-first -F -AhlS'
alias ds='du -d 1 -h 2>/dev/null | sort -h'
alias p='pwd'
alias path='echo -e ${PATH//:/\\n}'l,la,ll: To usegls, install GNU core utilities withbrew install coreutils.--colorcolorizes the output.--group-directories-firstputs directories before files.-Fadds/to directory names,@to symbolic links, and other type indicators.-Ashows all entries except.and...-huses human-readable file sizes.-lshows size, owner, group, permissions, and other details.-Ssorts by file size.
ds:du -d 1shows the size of entries one level below the current directory.2>/dev/nullhides error messages.sort -hsorts human-readable sizes through a pipe.
pprints the working directory, andpathprints one$PATHentry per line.
Short options can usually be combined: ls -AhlS is equivalent to ls -A -h -l -S.
Edit Files
alias v='vi'Replace vi with your preferred editor if necessary.
Search and Utilities
Search and Compare
fb() {
find . -size "+${2}M" -type f -name "$1" -exec ls -lhS {} +
}
rn() {
local extension=$1
local text=$2
local filename
for filename in *."$extension"; do
command mv -i -- "$filename" "${filename//$text/}"
done
}
dif() { git diff --no-index --color=always -- "$1" "$2" }
alias imgopt='open -a ImageOptim .'fb "*.pdf" 10 finds PDF files larger than 10 MB below the current directory:
-size "+${2}M"selects files larger than the second argument in MiB.-type fselects regular files.-name "$1"matches the first argument as a filename pattern.-exec ls -lhS {} +prints the results with readable sizes, largest first.
rn txt asdf renames .txt files by removing asdf from each filename. For example, aaasdfff.txt becomes aaff.txt.
Functions are defined as name() { commands } and can receive positional arguments:
$0is the function name.$1,$2, and so on are individual arguments."$@"expands to all arguments while preserving them as separate strings.$#is the number of arguments.$?is the exit status of the previous command.$$is the current shell's process ID.$!is the process ID of the most recent background command.
Other Shortcuts
alias his='history'
alias rl='exec ${SHELL} -l' # Reload the login shellCreate an Encrypted ZIP Archive
zipen() {
zip -er enc.zip "$@"
}zipen file1 file2 dir1 creates the password-protected archive enc.zip. Quoted "$@" preserves each supplied path as a separate argument, including paths that contain spaces.
Optional Command Overrides
The following aliases replace existing commands rather than introducing new names. They are convenient, but they change command behavior throughout the interactive shell.
alias cd='cs'
alias ls='gls --color --group-directories-first -F'
alias pwd='sed "s/ /\\\ /g" <<< ${PWD/#$HOME/"~"}'
alias cp='cp -iv'
alias mv='mv -iv'
alias rm='rm -iv'
alias grep='grep --color'
alias pip='pip3'
# Destructive shortcut: use only if you accept the risk.
alias rf='rm -rf'- The
cdalias lists directory contents after every successful move. - The
lsandpwdaliases replace standard output formats. - The
cp,mv, andrmaliases add confirmation and verbose output. - Replacing
pipcan interfere with the executable selected by a Python virtual environment;python -m pipis more explicit.
rf removes directories recursively without confirmation. Inspect the target carefully before using it, or omit this alias if an easy-to-type destructive shortcut is not worth the risk.
macOS Applications and Settings
Open Applications
alias hr='open .'
alias c='open /Applications/CotEditor.app'
alias vs='code'
alias chrome='open /Applications/Google\ Chrome.app'hropens the current directory in Finder.copens CotEditor.vsopens Visual Studio Code.chromeopens Google Chrome.
Show or Hide Hidden Files in Finder
alias show='defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder'
alias hide='defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder'You can also press Command + Shift + . in Finder to show or hide hidden files.
Hide or Show Desktop Icons
alias dhide='defaults write com.apple.finder CreateDesktop -bool false && killall Finder'
alias dshow='defaults write com.apple.finder CreateDesktop -bool true && killall Finder'Screenshot Settings
alias dwl='defaults write com.apple.screencapture location'
alias ddl='defaults delete com.apple.screencapture location'
alias drl='defaults read com.apple.screencapture location'For example, dwl ~/Desktop changes the screenshot destination to the Desktop.
Sleep Settings
alias sleepon='sudo pmset -a disablesleep 0'
alias sleepoff='sudo pmset -a disablesleep 1'Python
alias wpy='command -v python'
alias pin='python -m pip install'
alias puin='python -m pip uninstall'
alias pup='python -m pip install --upgrade pip'
alias pinreq='python -m pip install -r requirements.txt'
alias pf='python -m pip list --format=freeze'
alias pfr='python -m pip list --format=freeze > requirements.txt'Using python -m pip makes it explicit which Python interpreter owns the selected pip installation.
Activate or Deactivate a Virtual Environment
alias acv='source venv/bin/activate'
alias deac='deactivate'Git and GitHub
alias g='git'
alias ga='git add'
alias gb='git branch'
alias gc='git commit'
alias gch='git checkout'
alias gcl='git clone'
alias gd='git diff'
alias gf='git fetch'
alias gi='git init'
alias gm='git merge'
alias gps='git push'
alias gpl='git pull'
alias gpom='git push origin main'
alias gs='git status'Create a GitHub Repository and Its Initial Commit
The following function initializes the current directory and creates a repository through GitHub CLI:
# Usage: ginit private
# ginit public
ginit() {
local visibility=${1:-private}
git init
git add .
git commit -m "๐ Initial commit"
gh repo create --"$visibility" --source=. --push
}Install GitHub CLI before using the gh command.
Commit Messages with Emoji
# Add all changes, commit, and push the current branch.
gacp() { git add . && git commit -m "$*" && git push }
gini() { gacp "๐ Initial commit" }
gnew() { gacp "โจ NEW: $*" }
gimp() { gacp "๐ IMPROVE: $*" }
gprg() { gacp "๐ง PROGRESS: $*" }
gmtn() { gacp "๐ง MAINTAIN: $*" }
gfix() { gacp "๐ FIX: $*" }
ghot() { gacp "๐ HOTFIX: $*" }
gbrk() { gacp "โผ๏ธ BREAKING: $*" }
grem() { gacp "๐๏ธ REMOVE: $*" }
gmrg() { gacp "๐ MERGE: $*" }
gref() { gacp "โป๏ธ REFACTOR: $*" }
gtst() { gacp "๐งช TEST: $*" }
gdoc() { gacp "๐ DOC: $*" }
grls() { gacp "๐ RELEASE: $*" }
gsec() { gacp "๐ฎ SECURITY: $*" }
# Show the available commit types.
gtyp() {
local normal='\033[0;39m'
local green='\033[0;32m'
echo "$green gini$normal โ ๐ Initial commit
$green gnew$normal โ โจ NEW
$green gimp$normal โ ๐ IMPROVE
$green gprg$normal โ ๐ง PROGRESS
$green gmtn$normal โ ๐ง MAINTAIN
$green gfix$normal โ ๐ FIX
$green ghot$normal โ ๐ HOTFIX
$green gbrk$normal โ โผ๏ธ BREAKING
$green grem$normal โ ๐๏ธ REMOVE
$green gmrg$normal โ ๐ MERGE
$green gref$normal โ โป๏ธ REFACTOR
$green gtst$normal โ ๐งช TEST
$green gdoc$normal โ ๐ DOC
$green grls$normal โ ๐ RELEASE
$green gsec$normal โ ๐ฎ SECURITY"
}gacp stages every change in the repository. Run git status and review the diff before using it.
Commit-message References
- Jupyter Book Development Conventions
- How to Write a Git Commit Message
- Emoji-Log
- gitmoji-cli
- Emoji Cheat Sheet
- Complete List of GitHub Markdown Emoji Markup
- Commit Message Examples
Generate .gitignore
The following function downloads a .gitignore template from the gitignore.io API. The name gignore avoids conflicting with the gi='git init' alias above.
gignore() {
local IFS=,
curl -sL "https://www.toptal.com/developers/gitignore/api/$*"
}For example, run gignore macos python visualstudiocode > .gitignore.
Next Steps
For the recommended file layout, $ZDOTDIR, modular configuration files, and Zsh prompt customization, see Organizing Zsh Configuration. For the commands used inside these shortcuts, see the Command Line Guide.