Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Some notes I made while setting up ZSH for the first time
ZSH and Misc Command Line Notes
Handy Man Pages
zshexpn Expansion and substitution
zshmisc "Everything and then some"
Command Line Editing
ESC-. Insert last argument of previous history line. Repeat to go further back.
ESC-q Delete line and insert it again on next prompt to issue interim command.
ESC-' Quotes whole line
ESC-RETURN Insert literal newline. Easier to edit long lines.
cd OLD NEW Swap out OLD with NEW in current full path and try to cd there
cd -<TAB> Select dir from stack
mv FILE ~2 Move FILE to entry 2 on dir stack
### Aliases for 'dirs' and 'cd' (Auto-loaded from ~/.oh-my-zsh/lib/directories.zsh)
d Show 'dirs' directory stack with index numbers.
1,2,3,... cd to the corresponding entry in the directory stack
- cd back and forth between the top two dirs in the stack.
.. cd back one level
... cd back two levels
File Globbing
Use parentheses for grouping.
<5-12> Numbers from 5 to 12
<5-> Numbers from 5 and up
<-12> Numbers up to 12
<-> Any number
* Any string
? Any character
[...] Any character or character range in set
[^...] Inverted set
x|y x or y. Must be in parentheses.
x# Zero or more occurrences of x
x## One or more occurrences of x
**/ Recursive match for directories. Shorthand for (*/)#
***/ Like **/ but also follows symbolic links.
pat1~pat2 Matches if pattern 1 matches and pattern 2 does not match
### Globbing Qualifiers
Put at the end of the pattern, enclosed in parentheses.
/ Directories
. Plain files
@ Symbolic links
* Executable files
m[Mwhm][-|+]n Modified less/more than n days/months/weeks/hours/minutes ago
L[kKmM][-|+]n File size less/more than n bytes/kilobytes(k|K)/megabytes(m|M)
### Examples
**/*ViewController.[hm](m-5) All view controller files modified in the last 5 days
*(.L0) Zero-length regular files
**/*(.Lm+10) Files larger than 10 MB anywhere in hierarchy
*(m0) Files modified today
{x,y,z} x y z
{5..8} 5 6 7 8
=foo Full path of file foo
!! Previous command
!n History command number n
!-n Current command minus n
!str Most recent command starting with str
!?str[?] Most recent command containing str
!^ First argument of previous command
!$ Last argument of previous command
!:n N'th argument of previous command
!* All arguments of last command
!!:s/x/y/ Last command with first x replaced by y
!!:gs/x/y/ Last command with all x replaced by y
r Repeat last command
r x=y Repeat last command with x replaced by y
<(COMMAND) Readable pipe containing the output of COMMAND.
Example: ack <(ls DIR1) <(ls DIR2) # See files not in both dirs
$#array Number of elements in array
### Read a file into a variable
$ var="$(<file)"
### Copy a directory structure recursively without data/files
$ dirs=(**/*(/))
$ cd -- $dest_root
$ mkdir -p -- $dirs
### Quick for-each on files in a dir
$ for f (*) print -- $f
### Random numbers
$ echo $[${RANDOM}%1000] # random between 0-999
$ echo $[${RANDOM}%11+10] # random between 10-20
$ echo ${(l:3::0:)${RANDOM}} # N digits long (3 digits)
### Random array element
$ array = (*)
$ print $array[$RANDOM%$#array+1]
### Ping a range of hosts
$ for i in {1..254}; do ping -c 1 192.168.1.$i; done
### Add numerical prefix to all files using the default sort order
$ i=1; for j in *; do mv $j $i.$j; ((i++)); done
### Change extension on all matching files
Remember: zmv needs the glob patterns. Not the shell's expansion of them. Hence the quotes
$ zmv '(*).text' '$1.txt' # Equivalent lines. Use match group.
$ zmv -W '*.text' '*.txt' # Equivalent lines. Use wildcard to reference flag.
$ for f (*.text) mv $f $f:r.txt # :e :r :h :t
### Remove leading zeros from file extension
Remember: zmv needs the glob patterns. Not the shell's expansion of them. Hence the quotes
$ ls
filename.001 filename.003 filename.005 filename.007 filename.009
filename.002 filename.004 filename.006 filename.008 filename.010
$ zmv '(filename.)0##(?*)' '$1$2'
$ ls
filename.1 filename.10 filename.2 filename.3 filename.4 filename.5 filename.6 ...
### Lower/uppercase all matching files/dirs
$ zmv '(*)' '${(L)1}' # lowercase
$ zmv '(*)' '${(U)1}' # uppercase
### Rename pic1.jpg, pic2.jpg, ... to pic0001.jpg, pic0002.jpg, ...
$ zmv 'pic(*).jpg' 'pic${(l:4::0:)1}.jpg' # In current dir
$ zmv '(**/)pic(*).jpg' '$1/pic${(l:4::0:)2}.jpg' # Recursively
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment