It’s been over a year since the last installment of the series.
Sifting through others’ .zshrc, one occasionally finds aliases like:
alias ls=' ls' alias cd=' cd'… and so on. The reason for this is simple, but not obvious: with
setopt HIST_IGNORE_SPACE, these commands will be ignored, even if you don’t start these commands with a space.We already talked about brace expansion like
{1..5}which expands to1 2 3 4 5. But did you know you also can do{1..10..2}, which expands to1 3 5 7 9and{005..14..2}which expands to005 007 009 011 013? Oh, and{10..1}works as well as{10..1..2}. Now you can throw seq(1) away!Using C-r and C-s to search history is well known, but the default search is a bit limited. Use these lines to enable search by globs, e.g.
gcc*foo.c:bindkey "^R" history-incremental-pattern-search-backward bindkey "^S" history-incremental-pattern-search-forwardOne nice trick if you often suspend vi by C-z:
foreground-vi() { fg %vi } zle -N foreground-vi bindkey '^Z' foreground-viThis will make C-z on the command line resume vi again, so you can toggle between them easily. Even if you typed something already!
zsh has lots of documentation, but finding what you want to know can be difficult. The manpage zshall(1) contains everything, and this function will make it easy to search in:
zman() { PAGER="less -g -s '+/^ "$1"'" man zshall }Try
zman fcorzman HIST_IGNORE_SPACE! (Usenif the first match is not what you were looking for.)Recently, I’ve become an avid user of the directory stack, but not really for its intended usage; instead, I use it together with the next trick. Here’s how you can persist the dirstack across sessions:
DIRSTACKSIZE=9 DIRSTACKFILE=~/.zdirs if [[ -f $DIRSTACKFILE ]] && [[ $#dirstack -eq 0 ]]; then dirstack=( ${(f)"$(< $DIRSTACKFILE)"} ) [[ -d $dirstack[1] ]] && cd $dirstack[1] && cd $OLDPWD fi chpwd() { print -l $PWD ${(u)dirstack} >$DIRSTACKFILE }First, we limit the dirstack to nine entries, load them from
.zdirsif possible, and then we save them again on every directory change.For a long time, I used to have something similar that only saved
$OLDPWD, so I could open a new shell andcd -and be back where I last changed to. But now I use this, andAUTO_PUSHD, together with the next trick.Every zsh user knows that you can use
dirsto display the dirstack, andcd -N to go to the N-th element.But did you know zsh will show the dirstack on
cd -TAB? It’s awesome, and does all the directory jumping I need.% cd -TAB 1 -- /home/chris/mess/current 2 -- /home/chris/mess/current/mdnsd 3 -- /home/chris/mess/current/mdnsd/libutil 4 -- /home/chris 5 -- /home/chris/src/aewm-1.2.7/clients 6 -- /home/chris/mess/2011/47/fspanel-0.7 7 -- /home/chris/mess/2011/47 8 -- /home/chris/src/mcwmWhich words end with ‘tent’? Of course you can do
grep tent$ /usr/share/dict/words, but did you know you can dolook _tentand press TAB (_is where the cursor is)?This fantastic zsh trick is from Julius Plenz: complete words from tmux pane.
Perhaps you know
zmvalready, but it can be a bit nasty. E.g. to rename all*.lisfiles to*.txt, the manual recommends:zmv '(*).lis' '$1.txt'However, with the awesome
-Wmode, you can write this instead:zmv -W '*.lis' '*.txt'If you are not sure what happens, use the dry-run mode first (
-n).That concludes this, now hopefully yearly, installment. Perhaps you’ll find even more new more things in my recently cleaned up .zshrc. Enjoy your Z shell!
NP: EMA—Milkman