Back to Shell_Script

See Also bashUnix Utilities


Z Shell(Zsh) 是一种Unix shell,它可以用作为交互式的登录shell,也是一种强大的shell脚本命令解释器。Zsh可以认为是一种Bourne shell的扩展,带有数量庞大的改进,包括一些bash、ksh、tcsh的功能。

1. Origin

Zsh的最初版本是在1990年由Paul Falstad写成,当时他是普林斯顿大学的学生。

2. Features



2.1. 17 Shell Builtin Commands

2.2. 13 Prompt Expansion

$ echo $PROMPT

%{%}#%{%} %(#,%{%}%{%}%n%{%},%{%}%n) %{%}@ %{%}%m %{%}in %{%}%~%{%}$(ys_hg_prompt_info)$(git_prompt_info) %{%}[%*] %(?,,C:%{%}%?%{%})
%{%}$ %{%}
# hostname my-server-name

3. Installation

# Current Available Shells:
$ cat /etc/shells
# install zsh
$ sudo yum install zsh -y
# Set zsh as your default shell:
$ chsh -s /bin/zsh
Changing shell for liyan.
Shell changed.
# 安装oh my zsh
wget -O - | sh

4. oh-my-zsh

4.1. 配置(vi ~/.zshrc)

 26 # Uncomment the following line to disable bi-weekly auto-update checks.

 44 # Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
 45 # Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
 46 # Example format: plugins=(rails git textmate ruby lighthouse)
 47 # Add wisely, as too many plugins slow down shell startup.
 48 plugins=(git osx autojump sublime)

4.1.1. osx

tab 增强,quick-look filename 可以直接预览文件,man-preview grep 可以生成 grep手册 的pdf 版本等。


brew install zsh-syntax-highlighting && source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh 添加到 ~/.zshrc 尾端

4.1.2. autojump

autojump - a faster way to navigate your filesystem

智能跳转,安装了autojump之后,zsh 会自动记录你访问过的目录,通过 j + 目录名 可以直接进行目录跳转,而且目录名支持模糊匹配和自动补全,例如你访问过hadoop-1.0.0目录,输入j hado 即可正确跳转。j –stat 可以看你的历史路径库

brew install autojump
Add the following line to your ~/.bash_profile or ~/.zshrc file (and remember
to source the file to update your current session):
  [[ -s `brew --prefix`/etc/ ]] && . `brew --prefix`/etc/

➜  ~ autojump --help
                        add path
  -i [WEIGHT], --increase [WEIGHT]
                        increase current directory weight
  -d [WEIGHT], --decrease [WEIGHT]
                        decrease current directory weight
  --complete            used for tab completion
  --purge               remove non-existent paths from database
  -s, --stat            show database entries and their key weights
➜  ~ j svn
➜  svn j -d 100
0.0:    /opt/e_disk/doc/NetQin/2010-Monkey/svn
➜  svn j svn

4.1.3. sublime

插件中加入 sublime,然后命令行就有 st 命令了,比如在命令行用sublime打开一个java文件:st

alias -s c=st h=st # 在命令行直接输入后缀为 c 的文件名,会在 sublime 中打开

4.2. manually update


5. Themes

所有的主题存放在 ~/.oh-my-zsh/custom/themes 目录下,使用 ZSH_THEME 变量在 ~/.zshrc 中设置

Recommand Themes

5.1. agnoster

# Install a Powerline-patched font
git clone --depth=1
cd fonts && ./install
cd .. && rm -rf fonts
# Change font to "Meslo LG S Regular for Powerline 13 pt."

5.2. Terminal Themes macOS 原生终端 Terminal 主题,双击 xx.terminal 打开

6. Reference

CategorymacOS CategoryShell

MainWiki: Zsh (last edited 2011-04-23 04:10:22 by twotwo)