Zsh Cheat Sheet

Zsh (Z Shell) is a powerful shell with extensive customization capabilities. Here’s a cheat sheet to help you navigate and use Zsh effectively:

Basic Commands

Change Directory:

cd /path/to/directory

List Contents:

ls

Clear Screen:

clear

Exit Zsh:

exit

File Operations

Create a File:

touch filename

Create a Directory:

mkdir directoryname

Copy Files/Directories:

cp source destination

Move/Rename Files:

mv oldname newname

Remove Files/Directories:

rm filename

Navigation Shortcuts

Go to Home Directory:

cd ~

Go to Previous Directory:

cd -

Tab Completion:

  • Press Tab to autocomplete commands, file paths, etc.

History

View Command History:

history

Repeat Last Command:

!!

Search Command History:

history | grep keyword

Aliases

Create an Alias:

alias ll='ls -la'

List Aliases:

alias

Remove an Alias:

unalias ll

Prompt Customization

Change Prompt Theme:

ZSH_THEME="agnoster"   # Set to your preferred theme

Customize Prompt:

  • Edit ~/.zshrc to configure prompt settings.

Autocompletion

Enable Autocompletion:

autoload -U compinit && compinit

Plugins

Install Oh My Zsh Plugins:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Job Control

List Running Jobs:

jobs

Background a Job:

command &

Foreground a Job:

fg %job_number

Scripting

Create a Script:

  • Create a file with a .zsh extension and add your script.

Run a Script:

./script.zsh

Search Files

Find Files:

find /path/to/search -name "filename"

Zsh Options

Enable/Disable Globbing:

setopt noglob     # Disable
setopt globbing   # Enable

Case-Insensitive Tab Completion:

zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'

Directory Stack

Push to Directory Stack:

pushd /path/to/directory

Pop from Directory Stack:

popd

This cheat sheet covers basic Zsh commands and features. Zsh is highly customizable, so feel free to explore its extensive documentation for more advanced features and options.