国庆在家的时候把缺省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)
orRPROMPT = $'<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 promptpreexec
: 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 lineC-e
end of lineE-b
backward one ‘word’E-f
forward one “word”
- Deleting:
C-w
from cursor back to beginning of current wordE-d
the entire wordC-k
from the cursor to end of lineC-u
clear the whole line
- Modifying:
E-l
lowercaseE-u
uppercase, from cursor to end of current wordC-t
swap two charactersE-t
swap two wordsE-s
spell-correct current word
- History:
up
anddown
arrow keysE-<
andE->
first and most recent commandC-r
start a searching
- Buffer:
E-q
push current command into buffer and clear command lineE-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/linuxls -d /usr/src/linux/**/*(@)
lists all the symlinks in itls /usr/src/linux/**/*(*)
lists the executable files in itls /**/*(s)
lists all the setuid files on your system - great for a security auditls -d /**/*(u:neo:)
lists all the files owned by neols -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: