chris blogs

February 2012

17feb2012 · 10 new zsh tricks you may not know...

It’s been over a year since the last installment of the series.

  1. 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.

  2. We already talked about brace expansion like {1..5} which expands to 1 2 3 4 5. But did you know you also can do {1..10..2}, which expands to 1 3 5 7 9 and {005..14..2} which expands to 005 007 009 011 013? Oh, and {10..1} works as well as {10..1..2}. Now you can throw seq(1) away!

  3. 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-forward
    
  4. One nice trick if you often suspend vi by C-z:

    foreground-vi() {
      fg %vi
    }
    zle -N foreground-vi
    bindkey '^Z' foreground-vi
    

    This will make C-z on the command line resume vi again, so you can toggle between them easily. Even if you typed something already!

  5. 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 fc or zman HIST_IGNORE_SPACE! (Use n if the first match is not what you were looking for.)

  6. 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 .zdirs if 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 and cd - and be back where I last changed to. But now I use this, and AUTO_PUSHD, together with the next trick.

  7. Every zsh user knows that you can use dirs to display the dirstack, and cd -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/mcwm
    
  8. Which words end with ‘tent’? Of course you can do grep tent$ /usr/share/dict/words, but did you know you can do look _tent and press TAB (_ is where the cursor is)?

  9. This fantastic zsh trick is from Julius Plenz: complete words from tmux pane.

  10. Perhaps you know zmv already, but it can be a bit nasty. E.g. to rename all *.lis files to *.txt, the manual recommends:

    zmv '(*).lis' '$1.txt'
    

    However, with the awesome -W mode, 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

Copyright © 2004–2016