Skip to content

Instantly share code, notes, and snippets.

@petergi
Last active March 23, 2023 00:38
Show Gist options
  • Select an option

  • Save petergi/59567991c8ee26d379bd455bdb2a8d30 to your computer and use it in GitHub Desktop.

Select an option

Save petergi/59567991c8ee26d379bd455bdb2a8d30 to your computer and use it in GitHub Desktop.

ZSH Cheatsheet

Parameter expansion

files=(*.txt)          # store a glob in a variable
print -l $files
print -l $files(:h)    # this is the syntax we saw before
print -l ${files:h}
print -l ${files(:h)}  # don't mix the two, or you'll get an error
print -l ${files:u}    # the :u modifier makes the text uppercase

:s modifier

variable="path/aaabcd"
echo ${variable:s/bc/BC/}     # path/aaaBCd
echo ${variable:s_bc_BC_}     # path/aaaBCd
echo ${variable:s/\//./}      # path.aaabcd (escaping the slash \/)
echo ${variable:s_/_._}       # path.aaabcd (slightly more readable)
echo ${variable:s/a/A/}       # pAth/aaabcd (only first A is replaced)
echo ${variable:gs/a/A/}      # pAth/AAAbcd (all A is replaced)

Split the file name at each underscore

echo ${(s._.)file}

Join expansion flag, opposite of the split flag

array=(a b c d)
echo ${(j.-.)array}   # a-b-c-d

Short if

if [[ ... ]] command
if [[ ... ]] { command ... }

Short for

for i ( word ... ) command
for i ( word ... ) { command ... }
for i in word ... ; command

Short while/until

while [[ ... ]] { command ... }
until [[ ... ]] { command ... }

Use output of command, when using pipe is not possible

<( command )

Similar to <( ), but creates temporary file (instead of FD or FIFO), when program needs to seek in output.

=( command )

Expressions

Expression Example Description
!! sudo !! Last command (sudo !!)
!* vim !* Last command’s parameters (vim !*)
!^ Last command’s first parameter
!$ Last command’s last parameter
!?ls <tab> sudo !?mv <tab> Command and params of last ls command
!?ls?:* <tab> Params of last ls command
*(m0) rm *(m0) Last modified today
*(m-4) Last modified <4 days ago

Change default shell

chsh -s `which zsh`

Process Substitution

Expression Example Description
<(COMMAND) grep "needle" <(curl "https://haystack.io") Replace argument with named pipe/FIFO (read-only) with command output
=(COMMAND) vim =(curl "https://haystack.io") Replace argument with file (writable) containing command output

vi-mode

This plugin increase vi-like zsh functionality.

Use ESC or CTRL-[ to enter Normal mode.

History

  • ctrl-p : Previous command in history
  • ctrl-n : Next command in history
  • / : Search backward in history
  • n : Repeat the last /

Mode indicators

Normal mode is indicated with red <<< mark at the right prompt, when it wasn't defined by theme.

Vim edition

  • v : Edit current command line in Vim

Movement

  • $ : To the end of the line
  • ^ : To the first non-blank character of the line
  • 0 : To the first character of the line
  • w : [count] words forward
  • W : [count] WORDS forward
  • e : Forward to the end of word [count] inclusive
  • E : Forward to the end of WORD [count] inclusive
  • b : [count] words backward
  • B : [count] WORDS backward
  • t{char} : Till before [count]'th occurrence of {char} to the right
  • T{char} : Till before [count]'th occurrence of {char} to the left
  • f{char} : To [count]'th occurrence of {char} to the right
  • F{char} : To [count]'th occurrence of {char} to the left
  • ; : Repeat latest f, t, F or T [count] times
  • , : Repeat latest f, t, F or T in opposite direction

Insertion

  • i : Insert text before the cursor
  • I : Insert text before the first character in the line
  • a : Append text after the cursor
  • A : Append text at the end of the line
  • o : Insert new command line below the current one
  • O : Insert new command line above the current one

Delete and Insert

  • ctrl-h : While in Insert mode: delete character before the cursor
  • ctrl-w : While in Insert mode: delete word before the cursor
  • d{motion} : Delete text that {motion} moves over
  • dd : Delete line
  • D : Delete characters under the cursor until the end of the line
  • c{motion} : Delete {motion} text and start insert
  • cc : Delete line and start insert
  • C : Delete to the end of the line and start insert
  • r{char} : Replace the character under the cursor with {char}
  • R : Enter replace mode: Each character replaces existing one
  • x : Delete [count] characters under and after the cursor
  • X : Delete [count] characters before the cursor

Print Command

Command Description
print -l *.txt A plain old glob.
print -l **/*.txt A plain old glob.
print -l **/*<1-10>.txt Show text files that end in a number from 1 to 10
print -l **/[a]*.txt Show text files that start with the letter a
`print -l **/(ab bc)*.txt`
print -l **/[^cC]*.txt Show text files that don't start with a lower or uppercase c
print -l **/*(/) Show only directories
print -l **/*(.) Show only regular files
print -l **/*(L0) Show empty files.
print -l **/*(Lk+3) Show files greater than 3 KB
print -l **/*(mh-1) Show files modified in the last hour
print -l *.txt(:t:r) Return the file name without the extension
print -l *.txt(:e) Return the extension

Print Examples

# A plain old glob
print -l *.txt
print -l **/*.txt

# Show text files that end in a number from 1 to 10
print -l **/*<1-10>.txt

# Show text files that start with the letter a
print -l **/[a]*.txt

# Show text files that start with either ab or bc
print -l **/(ab|bc)*.txt

# Show text files that don't start with a lower or uppercase c
print -l **/[^cC]*.txt

# Show only directories
print -l **/*(/)

# Show only regular files
print -l **/*(.)

# Show empty files
print -l **/*(L0)

# Show files greater than 3 KB
print -l **/*(Lk+3)

# Show files modified in the last hour
print -l **/*(mh-1)

# Sort files from most to least recently modified and show the last 3
print -l **/*(om[1,3])

# `.` show files, `Lm-2` smaller than 2MB, `mh-1` modified in last hour,
# `om` sort by modification date, `[1,3]` only first 3 files
print -l **/*(.Lm-2mh-1om[1,3])

# Show every directory that contain directory `.git`
print -l **/*(e:'[[ -d $REPLY/.git ]]':)

# Return the file name (t stands for tail)
print -l *.txt(:t)

# Return the file name without the extension (r stands for remove_extension)
print -l *.txt(:t:r)

# Return the extension
print -l *.txt(:e)

# Return the parent folder of the file (h stands for head)
print -l *.txt(:h)

# Return the parent folder of the parent
print -l *.txt(:h:h)

# Return the parent folder of the first file
print -l *.txt([1]:h)

Also see

Zsh is mostly compatible with Bash, so most everything in Bash’s cheatsheet also applies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment