利用Oh-My-Zsh打造你的超級終端
什麼是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/