Catalog / Zsh Cheat Sheet

Zsh Cheat Sheet

A comprehensive cheat sheet for Zsh, covering essential commands, expressions, process substitution, and configuration tips for efficient command-line usage.

Zsh Basics

Shell Navigation

cd

Change directory to home.

cd <directory>

Change directory to <directory>.

cd -

Change to the previous directory.

pwd

Print the current working directory.

pushd <directory>

Push current directory onto stack and change to <directory>.

popd

Pop directory from stack and change to it.

File and Directory Management

ls

List files and directories in the current directory.

ls -l

List files with detailed information.

mkdir <directory>

Create a new directory named <directory>.

rm <file>

Remove the specified <file>.

rm -r <directory>

Recursively remove the directory <directory> and its contents.

cp <source> <destination>

Copy <source> to <destination>.

Basic Commands

echo <text>

Print <text> to the console.

cat <file>

Display the contents of <file>.

less <file>

View <file> one page at a time.

head <file>

Display the first 10 lines of <file>.

tail <file>

Display the last 10 lines of <file>.

grep <pattern> <file>

Search for <pattern> in <file>.

Expressions and History

History Expansion

!!

Execute the last command.

!<number>

Execute the command with the specified history number.

!$

The last word of the previous command.

!*

All words from the previous command (except the command name).

!^

The first argument of the previous command.

!-n

Execute the nth command from the end of the history list.

Filename Generation (Globbing)

*

Matches any string of characters.

?

Matches any single character.

[abc]

Matches a, b, or c.

[^abc]

Matches any character except a, b, or c.

[a-z]

Matches any character in the range a to z.

**/*

Recursively matches files in subdirectories.

Modifiers in History Expansion

s/foo/bar/

Substitute foo with bar.

&

Repeat the last substitution.

g

Global substitution.

h

Remove a trailing filename component, leaving only the head.

t

Remove all leading filename components, leaving the tail.

r

Remove a filename extension, leaving the root name.

Process Substitution & Redirection

Process Substitution

<(command)

Treat the output of command as a file for reading.

>(command)

Treat a file as the input to command.

Example:

diff <(ls dir1) <(ls dir2)

Example:

sort <(echo "b\na")

Example:

paste <(seq 3) <(seq 3)

Redirection

>

Redirect output to a file (overwrites).

>>

Redirect output to a file (appends).

2>

Redirect standard error to a file.

&>

Redirect both standard output and standard error to a file.

|

Pipe output of one command to the input of another.

tee <file>

Send output to both standard output and <file>.

Combining Redirection and Process Substitution

Example:

comm <(sort file1) <(sort file2)

Compares two sorted files and outputs the lines unique to each file and the lines common to both.

Example:

grep pattern <(zcat file.gz)

Grep through gzipped file without explicit extraction.

Example:

wc -l <(ls -l | grep "^-")

Counts the number of files in the current directory.

Zsh Configuration

Zsh Configuration Files

~/.zshrc - User-specific configuration file executed when a new shell is started interactively.

~/.zprofile - User-specific configuration file executed for login shells.

~/.zlogin - Alternative for ~/.zprofile, also executed for login shells.

~/.zlogout - User-specific configuration file executed when a login shell exits.

/etc/zsh/zshrc - System-wide configuration file for interactive shells.

/etc/zsh/zprofile - System-wide configuration file for login shells.

Options and Settings

setopt <option>

Enable a Zsh option.

unsetopt <option>

Disable a Zsh option.

bindkey <key> <command>

Bind a key sequence to a Zsh command.

alias <name>=<command>

Create an alias for a command.

export VAR=value

Set an environment variable.

source <file>

Execute commands from <file> in the current shell.

Example Configuration Snippets

Enable command autocorrection:

setopt correct

Set up a custom alias:

alias la='ls -la'

Configure history settings:

 HISTSIZE=10000
SAVEHIST=10000

Enable vi mode:

 bindkey -v