Zsh: tab complete glob qualifiers

Me remembering how to use Zsh globs before I learned this tip.

I’ve written a few posts on Zsh’s glob qualifiers recently, covering:

These single-character codes are powerful, but dense and forgettable. Luckily, you don’t need to memorize them at all exactly, because Zsh has a built-in cheat sheet: tab completion within the qualifier parentheses.

Type a glob, an opening parenthesis, and press Tab:

$ print -l *(<Tab>

Zsh then lists every available qualifier, each with a short description:

-  -- follow symlinks toggle
:  -- modifier
.  -- plain files
)  -- end of qualifiers
[  -- + range of files
@  -- symbolic links
*  -- executable plain files
/  -- directories
#  -- introduce glob flag
%  -- device files
^  -- negate qualifiers
+  -- + command name
=  -- sockets
a  -- + access time
A  -- group-readable
c  -- + inode change time
d  -- + device
D  -- glob dots
e  -- execute code
E  -- group-executable
f  -- + access rights
F  -- non-empty directories
g  -- + owning group
G  -- owned by EGID
I  -- group-writeable
l  -- + link count
L  -- + size
m  -- + modification time
M  -- mark directories
n  -- numeric glob sort
N  -- use NULL_GLOB
O  -- + sort order, down
o  -- + sort order, up
p  -- named pipes (FIFOs)
P  -- prepend word
r  -- owner-readable
R  -- world-readable
S  -- setgid
s  -- setuid
T  -- mark types
t  -- sticky bit set
u  -- + owning user
U  -- owned by EUID
w  -- owner-writeable
W  -- world-writeable
x  -- owner-executable
X  -- world-executable
Y  -- + at most ARG matches

It’s a lot, but hopefully skimmable.

Entries marked with + take an argument. For example, m needs a number of days, as in (m0), and [ needs a range, as in [1,5].

This list is a handy reminder of what’s possible, and you may even find it more readable than the glob qualifier docs.

Completion keeps going

Completion doesn’t stop after the first qualifier. Press Tab again after some qualifiers and Zsh offers the next step. For example, after o or O, it lists the sort specifiers:

$ print -l *(o<Tab>
+  -- + command name
a  -- last access time
c  -- last inode change time
d  -- directory depth
e  -- execute code
l  -- number of hard links
L  -- size of file
m  -- last modification time
n  -- lexical order of name
N  -- no sorting

…and after :, it lists the filename modifiers, as I covered recently:

$ print -l *(:<Tab>
&  -- repeat substitution
a  -- absolute path, resolve '..' lexically
A  -- as ':a', then resolve symlinks
c  -- PATH search for command
e  -- leave only extension
g  -- globally apply s or &
h  -- head - strip trailing path element
l  -- lower case all words
P  -- realpath, resolve '..' physically
Q  -- strip quotes
r  -- root - strip suffix
s  -- substitute string
t  -- tail - strip directories
u  -- upper case all words

Completion needs to be enabled

This tab completion comes from Zsh’s completion system, which thus needs to be enabled. It’s enabled by default in most setups, including with my preferred Oh My Zsh, but if yours isn’t working, try adding this to your ~/.zshrc:

autoload -Uz compinit
compinit

Fin

Glob qualifier tab completions? I just think they’re neat. Thanks to xro’s hackmas talk for showing me this feature (the talk is in German but has English subtitles available).

May you be visited by many tabby cats,

—Adam


Read my book Boost Your Git DX to Git better.


Subscribe via RSS, Twitter, Mastodon, or email:

One summary email a week, no spam, I pinky promise.

Related posts:

Tags: