Back to Shell_Script

See Also bashUnix Utilities

Zsh

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

1. Origin

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

2. Features

Zsh具有以下主要功能:

zsh的手册里非常有名的第一句“鉴于zsh有许多功能,手册被分为数个章节”,总计有17章节,可见其手册的长度。

2.1. 17 Shell Builtin Commands

http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html

2.2. 13 Prompt Expansion

http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html

$ 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.
Password: 
Shell changed.
# 安装oh my zsh
wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

4. oh-my-zsh

4.1. 配置(vi ~/.zshrc)

 26 # Uncomment the following line to disable bi-weekly auto-update checks.
 27 DISABLE_AUTO_UPDATE="true"

 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/autojump.sh ]] && . `brew --prefix`/etc/autojump.sh

➜  ~ autojump --help
  -a DIRECTORY, --add DIRECTORY
                        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
/opt/e_disk/doc/NetQin/2012-Hesine/svn
➜  svn j -d 100
0.0:    /opt/e_disk/doc/NetQin/2010-Monkey/svn
➜  svn j svn
/opt/e_disk/doc/NetQin/2012-Pig/svn

4.1.3. sublime

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

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

4.2. manually update

upgrade_oh_my_zsh

5. Themes

https://github.com/robbyrussell/oh-my-zsh/wiki/Themes/https://github.com/robbyrussell/oh-my-zsh/wiki/External-themes

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

Recommand Themes

5.1. agnoster

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

5.2. Terminal Themes

https://github.com/mbadolato/iTerm2-Color-Schemes/tree/master/terminal macOS 原生终端 Terminal 主题,双击 xx.terminal 打开

6. Reference


CategorymacOS CategoryShell

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