什么是Zsh
Zsh是一款強大的虛擬終端,既是一個系統的虛擬終端,也可以作為一個腳本語言的交互解析器。
Zsh的一些特性
兼容bash,原來使用bash的兄弟切換過來毫無壓力。
強大的歷史紀錄功能,在用或者方向上鍵查找歷史命令時,zsh支持限制查找。比如輸入ls然后再按方向上鍵,則只會查找用過的ls命令。而此時使用則會仍然按之前的方式查找,忽略ls。
多個終端會話共享歷史記錄。經常有多個窗口,tab,tmux的多個session,panel。這些命令歷史不能共享實在是很糟糕的回憶。但是有了zsh之后,這些確實成了回憶了,所有的命令歷史都可以共享。
智能拼寫糾正,輸入
gtep mactalk * -R,系統會提示:zsh: correct 'gtep' to 'grep' [nyae]?比妹紙貼心吧,她們向來都是讓你猜的……各種補全:路徑補全、命令補全,命令參數補全,插件內容補全等等。觸發補全只需要按一下或兩下tab鍵,補全項可以使用ctrl+n/p/f/b上下左右切換。比如你想殺掉java的進程,只需要輸入
kill java + tab鍵,如果只有一個java進程,zsh 會自動替換為進程的 pid,如果有多個則會出現選擇項供你選擇。ssh+空格+兩個tab鍵,zsh會列出所有訪問過的主機和用戶名進行補全智能跳轉,安裝了autojump之后,zsh 會自動記錄你訪問過的目錄,通過 j + 目錄名 可以直接進行目錄跳轉,而且目錄名支持模糊匹配和自動補全,例如你訪問過hadoop-1.0.0目錄,輸入j hado 即可正確跳轉。j –stat 可以看你的歷史路徑庫。
目錄瀏覽和跳轉:輸入 d,即可列出你在這個會話里訪問的目錄列表,輸入列表前的序號,即可直接跳轉。
在當前目錄下輸入 .. 或 … ,或直接輸入當前目錄名都可以跳轉,你甚至不再需要輸入cd命令了。
通配符搜索:
ls -l **/*.sh,可以遞歸顯示當前目錄下的 shell 文件,文件少時可以代替find,文件太多還是用find。
Zsh默認不兼容Bash,不過Zsh實現中包含了一個屌炸天的仿真模式(emulation mode)支持對兩種主流的Bourne衍生版shell(bash、ksh)和C shell的仿真(csh的支持並不完整)。在Bash的仿真模式下,可以使用與Bash相同的語法和命令集合,從而達到近乎完全兼容的目的。
為了激活對Bash的仿真,需要顯式執行
$ emulate bash
等效於
$ emulate sh
更詳細介紹可到官網了解,zsh官網:http://www.zsh.org/
安裝
Redhat/Centos
$ yum install zsh
Debian/Ubuntu
$ apt-get install zsh
設置默認shell為zsh
$ chsh -s $(which zsh)
完成后可以使用下面這條命令檢查是否設置成功
$ echo $SHELL
什么是Oh My Zsh
Oh My Zsh是一款社區驅動的命令行工具,正如它的主頁上說的,Oh My Zsh 是一種生活方式。它基於zsh命令行,提供了主題配置,插件機制,已經內置的便捷操作。給我們一種全新的方式使用命令行。
Oh My Zsh這個名字聽起來就很有意思,它是基於zsh命令行的一個擴展工具集,提供了豐富的擴展功能。
Oh My Zsh只是一個對zsh命令行環境的配置包裝框架,但它不提供命令行窗口,更不是一個獨立的APP。
更詳細介紹可到官網了解,Oh My Zsh官網:http://ohmyz.sh
安裝Oh My Zsh
前提條件:必須已安裝ZSH
我們可以在命令行中輸入這個命令來查看我們的電腦上是否安裝了zsh命令行
$ zsh --version
如果安裝了,這個命令就會輸出zsh當前的版本號。確定 zsh 已經安裝后,我們就可以繼續安裝Oh My Zsh了。
安裝Oh My Zsh
Oh My Zsh 的安裝方式非常簡單,可以通過curl或wget的方式,用一條命令即可安裝。
curl 方式
$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
wget 方式
$ sh -c "$(wget https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
安裝過程中輸出如下
Cloning Oh My Zsh...
Cloning into '/root/.oh-my-zsh'...
remote: Counting objects: 712, done.
remote: Compressing objects: 100% (584/584), done.
remote: Total 712 (delta 15), reused 522 (delta 4), pack-reused 0
Receiving objects: 100% (712/712), 443.58 KiB | 27.00 KiB/s, done.
Resolving deltas: 100% (15/15), done.
Checking connectivity... done.
Looking for an existing zsh config...
Using the Oh My Zsh template file and adding it to ~/.zshrc
Copying your current PATH and adding it to the end of ~/.zshrc for you.
Time to change your default shell to zsh!
__ __
____ / /_ ____ ___ __ __ ____ _____/ /_
/ __ \/ __ \ / __ `__ \/ / / / /_ / / ___/ __ \
/ /_/ / / / / / / / / / / /_/ / / /_(__ ) / / /
\____/_/ /_/ /_/ /_/ /_/\__, / /___/____/_/ /_/
/____/ ....is now installed!
Please look over the ~/.zshrc file to select plugins, themes, and options.
p.s. Follow us at https://twitter.com/ohmyzsh.
p.p.s. Get stickers and t-shirts at http://shop.planetargon.com.
到這里,Oh My Zsh 就安裝成功了。
Oh My Zsh目錄結構
進入~/.oh-my-zsh目錄后,看看該目錄的結構
$ ls ~/.oh-my-zsh
cache custom lib log MIT-LICENSE.txt oh-my-zsh.sh plugins README.markdown templates themes tools
lib 提供了核心功能的腳本庫
tools 提供安裝、升級等功能的快捷工具
plugins 自帶插件的存在放位置
templates 自帶模板的存在放位置
themes 自帶主題文件的存在放位置
custom 個性化配置目錄,自安裝的插件和主題可放這里
設置主題
安裝完畢后,我們就可以使用了,咱們先來簡單配置一下。
Oh My Zsh 提供了很多主題風格,我們可以根據自己的喜好,設置主題風格,主題的配置在 ~/.zshrc 文件中可以看到,用一個自己熟悉的編輯器打開這個文件,可以找到這一項:
ZSH_THEME="robbyrussel"
可以看到,我們默認使用的主題叫做robbyrussel。 它的顯示效果嘛,大概是這樣

Oh My Zsh默認自帶了一些默認主題,存放在~/.oh-my-zsh/themes目錄中。我們可以查看這些主題
$ ls ~/.oh-my-zsh/themes
3den.zsh-theme essembeh.zsh-theme junkfood.zsh-theme rgm.zsh-theme
Soliah.zsh-theme evan.zsh-theme kafeitu.zsh-theme risto.zsh-theme
adben.zsh-theme example.zsh-theme kardan.zsh-theme rixius.zsh-theme
af-magic.zsh-theme fino-time.zsh-theme kennethreitz.zsh-theme rkj-repos.zsh-theme
afowler.zsh-theme fino.zsh-theme kiwi.zsh-theme rkj.zsh-theme
agnoster.zsh-theme fishy.zsh-theme kolo.zsh-theme robbyrussell.zsh-theme
alanpeabody.zsh-theme flazz.zsh-theme kphoen.zsh-theme sammy.zsh-theme
amuse.zsh-theme fletcherm.zsh-theme lambda.zsh-theme simonoff.zsh-theme
apple.zsh-theme fox.zsh-theme linuxonly.zsh-theme simple.zsh-theme
arrow.zsh-theme frisk.zsh-theme lukerandall.zsh-theme skaro.zsh-theme
....
怎么樣,默認主題的數量相當多吧,這就是社區驅動的力量。
這么多的主題可以滿足你的各種偏好了,我們只需在./zshrc文件中配置需要的主題即可,比如我們想用lambda.zsh-theme這個主題,我們只要這樣配置
ZSH_THEME="lambda"
然后保存這個文件文件,再打開一個新的命令行窗口即可看到效果了。那么這么多的主題如何快速找到哪些是我們喜歡的呢,我們還可以這樣設置
ZSH_THEME="random"
我們將主題設置為隨機,這樣在我們每次打開命令行窗口的時候,都會隨機在默認主題中選擇一個。這樣我們如果覺得當前的主題比較喜歡,可以直接使用 echo 命令輸出當前主題的名稱
$ echo $ZSH_THEME
然后再將他設置到配置文件中即可。
如果這些默認主題還不能滿足你的需要,我們還可以到這里找到更多的主題
https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
https://github.com/robbyrussell/oh-my-zsh/wiki/External-themes
https://github.com/unixorn/awesome-zsh-plugins#themes
啟用插件
Oh My Zsh 默認自帶了一些默認主題,存放在~/.oh-my-zsh/plugins目錄中。我們可以查看這些插件
$ ls ~/.oh-my-zsh/plugins
adb brew coffee dirpersist fastfile gitignore httpie last-working-dir nanoc pod rebar sprunge terminitor vault zeus
ant brew-cask colemak django fbterm git-prompt iwhois lein nmap postgres redis-cli ssh-agent terraform vim-interaction zsh-navigation-tools
apache2-macports bundler colored-man-pages dnf fedora git-remote-branch jake-node lighthouse node pow repo stack textastic vi-mode zsh_reload
archlinux bwana colorize docker forklift glassfish jhbuild lol npm powder rsync sublime textmate virtualenv
asdf cabal command-not-found docker-compose frontend-search gnu-utils jira macports nvm powify ruby sudo thefuck virtualenvwrapper
autoenv cake common-aliases emacs gas go jruby man nyan profiles rvm supervisor themes vundle
autojump cakephp3 compleat ember-cli geeknote golang jsontools marked2 osx pyenv safe-paste suse thor wakeonlan
autopep8 capistrano composer emoji gem gpg-agent jump mercurial pass pylint sbt svn tmux wd
aws cask copydir emoji-clock git gradle kate meteor paver python scala svn-fast-info tmux-cssh web-search
battery catimg copyfile emotty git-extras grails kitchen mix pep8 rails scd symfony tmuxinator wp-cli
bbedit celery cp encode64 gitfast grunt knife mix-fast per-directory-history rake screen symfony2 torrent xcode
bgnotify chruby cpanm extract git-flow gulp knife_ssh mosh perl rake-fast scw systemadmin tugboat yii
boot2docker chucknorris debian fabric git-flow-avh heroku laravel mvn phing rand-quote sfffe systemd ubuntu yii2
bower cloudapp dircycle fancy-ctrl-z github history laravel4 mysql-macports pip rbenv singlechar taskwarrior urltools yum
branch codeclimate dirhistory fasd git-hubflow history-substring-search laravel5 n98-magerun pj rbfu spring terminalapp vagrant z
Oh My Zsh默認是只啟用git插件
plugins=(git)
如需啟用更多插件,可加入需啟用插件的名稱。如下
plugins=(git wd web-search history history-substring-search)
推薦幾個好用插件
zsh-history-substring-search
https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/history-substring-searchzsh-syntax-highlighting
https://github.com/zsh-users/zsh-syntax-highlightingzsh-autosuggestions
https://github.com/zsh-users/zsh-autosuggestions
更多插件可參考
https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins
https://github.com/unixorn/awesome-zsh-plugins
https://github.com/robbyrussell/oh-my-zsh/wiki/Plugins-Overview
一些小技巧
給history命令增加時間
.zshrc中加入以下行
$ vim ~/.zshrc
HIST_STAMPS="yyyy-mm-dd"
source ~/.zshrc
如果沒用oh my zsh的話可用如下alias
$ alias history='fc -il 1'
更新oh-my-zsh
設置自動更新oh-my-zsh
默認情況下,當oh-my-zsh有更新時,都會給你提示。如果希望讓oh-my-zsh自動更新,在~/.zshrc 中添加下面這句
DISABLE_UPDATE_PROMPT=true
要手動更新,可以執行
$ upgrade_oh_my_zsh
卸載oh my zsh
直接在終端中,運行uninstall_oh_my_zsh既可以卸載。
參考文檔
http://www.google.com
http://ohmyz.sh/
http://swiftcafe.io/2015/10/31/cafe-time-omz/
