2022-10-18

Useful Zsh Aliases and Functions

Practical shortcuts for files, macOS, Python, and Git

Table of Contents

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.

AliasFunction
Best forA fixed command or fixed optionsArguments or multiple commands
Example useReplace h with cd ~Create a directory and enter it
Definitionalias 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.

Note

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 ~/.zshrc

For 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 featureRequirement
glsGNU core utilities: brew install coreutils
open, osascript, defaults, pmsetmacOS
codeVisual Studio Code command-line launcher
imgoptImageOptim
ghGitHub CLI
git, curl, zipInstall separately if unavailable

Copy only the shortcuts that match your tools and workflow. Test each addition in a new terminal before relying on it.

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 use gls, install GNU core utilities with brew install coreutils.
    • --color colorizes the output.
    • --group-directories-first puts directories before files.
    • -F adds / to directory names, @ to symbolic links, and other type indicators.
    • -A shows all entries except . and ...
    • -h uses human-readable file sizes.
    • -l shows size, owner, group, permissions, and other details.
    • -S sorts by file size.
  • ds: du -d 1 shows the size of entries one level below the current directory.
    • 2>/dev/null hides error messages.
    • sort -h sorts human-readable sizes through a pipe.
  • p prints the working directory, and path prints one $PATH entry per line.
Tip

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 f selects 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 and Arguments

Functions are defined as name() { commands } and can receive positional arguments:

  • $0 is 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 shell

Create 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 cd alias lists directory contents after every successful move.
  • The ls and pwd aliases replace standard output formats.
  • The cp, mv, and rm aliases add confirmation and verbose output.
  • Replacing pip can interfere with the executable selected by a Python virtual environment; python -m pip is more explicit.
Warning

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'
  • hr opens the current directory in Finder.
  • c opens CotEditor.
  • vs opens Visual Studio Code.
  • chrome opens 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'
Tip

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"
}
Warning

gacp stages every change in the repository. Run git status and review the diff before using it.

Commit-message References

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.

Related posts