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 uppercasevariable="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)echo ${(s._.)file}array=(a b c d)
echo ${(j.-.)array} # a-b-c-dif [[ ... ]] command
if [[ ... ]] { command ... }for i ( word ... ) command
for i ( word ... ) { command ... }
for i in word ... ; commandwhile [[ ... ]] { command ... }
until [[ ... ]] { command ... }<( command )Similar to <( ), but creates temporary file (instead of FD or FIFO), when program needs to seek in output.
=( command )| 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 |
chsh -s `which zsh`| 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 |
This plugin increase vi-like zsh functionality.
Use ESC or CTRL-[ to enter Normal mode.
ctrl-p: Previous command in historyctrl-n: Next command in history/: Search backward in historyn: Repeat the last/
Normal mode is indicated with red <<< mark at the right prompt, when it wasn't defined by theme.
v: Edit current command line in Vim
$: To the end of the line^: To the first non-blank character of the line0: To the first character of the linew: [count] words forwardW: [count] WORDS forwarde: Forward to the end of word [count] inclusiveE: Forward to the end of WORD [count] inclusiveb: [count] words backwardB: [count] WORDS backwardt{char}: Till before [count]'th occurrence of {char} to the rightT{char}: Till before [count]'th occurrence of {char} to the leftf{char}: To [count]'th occurrence of {char} to the rightF{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
i: Insert text before the cursorI: Insert text before the first character in the linea: Append text after the cursorA: Append text at the end of the lineo: Insert new command line below the current oneO: Insert new command line above the current one
ctrl-h: While in Insert mode: delete character before the cursorctrl-w: While in Insert mode: delete word before the cursord{motion}: Delete text that {motion} moves overdd: Delete lineD: Delete characters under the cursor until the end of the linec{motion}: Delete {motion} text and start insertcc: Delete line and start insertC: Delete to the end of the line and start insertr{char}: Replace the character under the cursor with {char}R: Enter replace mode: Each character replaces existing onex: Delete [count] characters under and after the cursorX: Delete [count] characters before the cursor
| 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 |
# 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)
Zsh is mostly compatible with Bash, so most everything in Bash’s cheatsheet also applies.