It’s been almost three years since the last installment, so here is the next dollop of tips:
=(command)
expands to a tempfile with the output of command
that is deleted after the line has finished. In effect, the
same as <(command)
but allows applications to seek. E.g.:
xpdf =(zcat foo.pdf.gz)
!
-history-expansion is nice, but can be confusing if you have a
command line with many ! that should be left alone. Either quote the
!
with single quotes or write !"
at the beginning of the line
(yes, that "
is left unclosed):
% !" echo Hey there! Wow!!
Hey there! Wow!!
An application of modifiers is !:t
, which results into the
basename of the last argument. Very useful when working with URLs,
for example. You’ll never have to strip the path manually again:
% wget ftp://ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p330.tar.gz
% tar xzvf !:t
When playing with parameter expansion flags, it often is annoying having to use variables for immediate values:
% foo=bar.c; echo ${foo:a:u}
/HOME/CHRIS/BAR.C
Instead of the ugly solution
% echo ${$(echo bar.c):a:u}
better use this:
% echo ${${:-bar.c}:a:u}
Here, ${:-bar.c} is an instance of the well-known ${FOO:-BAR}
default substition operator.
To run a command several times, use repeat
. Useful for benchmarks, e.g.:
% repeat 3 time sleep 1
sleep 1 0.00s user 0.00s system 0% cpu 1.002 total
sleep 1 0.00s user 0.00s system 0% cpu 1.005 total
sleep 1 0.00s user 0.00s system 0% cpu 1.002 total
Use glob modifiers to sort glob expansions. Helpful are: (om)
(sort by modification time) or (n)
(sort numerically):
% pdfjoin chapter*.pdf(n) -o all.pdf
Another useful glob modifier is P
, for example to prefix a flag:
% tar czvf foo.tar.gz * *.tmp(P:--exclude:)
(yes, tar
can exclude patterns, but some other tools can’t, and zsh
does patterns better anyway.)
Some ZLE hacks I use. To override default completion in various ways:
# Force file name completion on C-x TAB, Shift-TAB.
zle -C complete-files complete-word _generic
zstyle ':completion:complete-files:*' completer _files
bindkey "^X^I" complete-files
bindkey "^[[Z" complete-files
# Force menu on C-x RET.
zle -C complete-first complete-word _generic
zstyle ':completion:complete-first:*' menu yes
bindkey "^X^M" complete-first
A function to make adding flags or prefixing arguments easier:
# Move to where the arguments belong.
after-first-word() {
zle beginning-of-line
zle forward-word
}
zle -N after-first-word
bindkey "^X1" after-first-word
Complete with words in the history (like Emacs dabbrev):
# Complete in history with M-/, M-,
zstyle ':completion:history-words:*' list no
zstyle ':completion:history-words:*' menu yes
zstyle ':completion:history-words:*' remove-all-dups yes
bindkey "\e/" _history-complete-older
bindkey "\e," _history-complete-newer
Of course, all things are mentioned in the comprehensive manual, or the great User’s Guide to the Z-Shell which I wholeheartedly recommend. But one needs to find them. :)
NP: Aimee Mann—Freeway