国庆在家的时候把缺省shell换成了zsh,主要是想看看这个备受推崇的shell有什么特别。这个东西不知道是太新还是太复杂,到现在满世界都没有一份可用的cheatsheet,不过找到了普林斯顿的Kevin McCarty的一份介绍(主页说他现在已经不在普大了),算是有其他shell基础的同学进入zsh的不错的指引,下面是我整理的学习笔记——似乎以此基础做张cheatsheet不是很难了 XD

目前来说,我最喜欢的部分是通配符(wild-cards)和可编程的自动完成(completion)。

在Mac OS X 10.5下改变缺省shell的办法是:打开System Preferences – Accounts,点左下角的锁并输入你的用户密码来进入sudo模式,然后右键或者按住Ctrl键点击你的用户名,会弹出菜单Advanced Options,里面的Login Shell选择即可。

% zsh --version
zsh 4.3.4 (i386-apple-darwin9.0)

The Prompt

  • Environment: PROMPT(PS1) or RPROMPT = $'<The setting string>'
  • Simple formatting: %Uunderline%u %Bbold%b
  • Coloring: %{\e[0;31m%} red | %{\e[0;34m%} blue | %{\e[0m%} none
  • Variables:
    • %m the machine name
    • %n the current username
    • %~ the current working directory
    • %h the number of the current command in the shell history
    • %t the current time
    • %% … the ‘%’ >_<
  • More: man zshmisc

The Hooks

  • precmd: before printing a new prompt
  • preexec: before running a command
  • Using these to control the xterm title:
    
    # make sure we're in an xterm!
    case $TERM in (xterm*|rxvt)
    	precmd () { print -Pn "\e]0;%n@%m: %~\a" }
    	preexec () { print -Pn "\e]0;%n@%m: $*\a" }
    	;;
    esac
    # The -P argument of print command tells it to expand the %n and %m
    # sequences as if they were in a PROMPT variable

Command-line Editing[1. Emacs-style key-bindings: C for Ctrl and E for META/ESC]

  • Moving:
    • C-a beginning of line
    • C-e end of line
    • E-b backward one ‘word’
    • E-f forward one “word”
  • Deleting:
    • C-w from cursor back to beginning of current word
    • E-d the entire word
    • C-k from the cursor to end of line
    • C-u clear the whole line
  • Modifying:
    • E-l lowercase E-u uppercase, from cursor to end of current word
    • C-t swap two characters
    • E-t swap two words
    • E-s spell-correct current word
  • History:
    • up and down arrow keys
    • E-< and E-> first and most recent command
    • C-r start a searching
  • Buffer:
    • E-q push current command into buffer and clear command line
    • E-h do the same and open man page
  • Key binding customization to make HOME/END work[2. To find out the code for HOME/END key, C-v and press corresponding key.]:
    
    bindkey '\e[1~' beginning-of-line
    bindkey '\e[4~' end-of-line
    case $TERM in (xterm*)
    	bindkey '\e[H' beginning-of-line
    	bindkey '\e[F' end-of-line ;;
    esac
  • Try: vared PATH
  • More: man zshzle man zshmodules

Wild-cards

  • Try: grep Linus /usr/src/linux/**/*.[ch]
  • If failed, try[3. The -N argument to "print" and the -0 (zero) argument to "xargs" ensure that filenames that include spaces do not cause trouble.]:
    print -N /usr/src/linux/**/*.[ch] | xargs -0 grep Linus
  • ls -d /usr/src/linux/**/*(/) lists all the directories in /usr/src/linux
  • ls -d /usr/src/linux/**/*(@) lists all the symlinks in it
  • ls /usr/src/linux/**/*(*) lists the executable files in it
  • ls /**/*(s) lists all the setuid files on your system - great for a security audit
  • ls -d /**/*(u:neo:) lists all the files owned by neo
  • ls -d /**/*(m-10) lists all the files modified within the last 10 days
  • To make it work, check .zshrc: setopt extended_glob
  • More: man zshexpn

Completion

  • Check .zshrc to enable basic completion:[4. The last command sets the zsh completions to follow the same color scheme as ls --color command. You can use ZLS_COLOURS instead if you are feeling British XD]
    
    complist
    autoload -U compinit
    compinit
    ZLS_COLORS=$LS_COLORS
  • Try: finger [press TAB key]
  • C-u to cancel

Aliases

  • Global alias:
    
    alias -g '...'='../..'
    alias -g '....'='../../..'
    alias -g '.....'='../../../..'
    # continuing until you get bored
  • Path alias:
    
    hash -d mrp="/Users/neo/Code/Repo/mrp"
    cd ~mrp

Notes: