?
使用 Zsh 的九個理由
像大部分 *nix 用戶,我之前用 bash 很多年,期間也有過小的不爽,但一直都忍過來,或者是說沒想過這些不爽的地方能解決,比如
cd
到一個深目錄時得哐哐猛敲
<TAB>
。這么多年里我也嘗試過其他 shell。比如 ksh/tcsh 以及今天要說的 zsh,但最終都沒堅持下去,因為心中始終還是認為 bash 是最正統(tǒng)的 shell,不愿意去主動深入學習其他 shell。直到前幾天逛 github,發(fā)現(xiàn)
排名第 6 的開源項目 oh-my-zsh
,下來試用了一把,頓時覺得 bash 各種操作不爽到無法忍受。
?
放棄 bash 的各種內(nèi)牛滿面的理由
這里有個 youtube 上的視頻,短短 4 分鐘就已經(jīng)拋出了幾十個讓 bash 用戶切換到 zsh 中的理由。 視頻鏈接
?
理由 0:zsh 兼容 bash
兼容 bash 意味著我不需要太多學習成本就可以切換過來,意味著我以前在 bash 下積累的 shell 語法、基本操作都不會荒廢。在我心里 bash 還是最通用和標準的 shell 環(huán)境,因此兼容 bash 讓我切換到 zsh 時沒有太多后顧之憂。
?
理由 1:zsh 的補全模式更方便
zsh 中按兩下 tab 鍵可以觸發(fā) zsh 的補全,所有待補全項都可以通過鍵盤方向鍵或者
<Ctrl-n/p/f/b>
來選擇。
?
理由 2:zsh 支持命令選項補全
zsh 除了支持目錄的補全,還支持命令選項的補全,例如
ls -<TAB><TAB>
會直接列出所有
ls
的參數(shù),再也不會出現(xiàn)一個命令打到一半,忘記參數(shù)導致重開一個 terminal
man
一把。
?
理由 3:zsh 支持命令參數(shù)補全
以前想
kill
掉一個進程,我的做法是
ps aux | grep "進程名"
然后記下 id,再
kill id
。在
zsh
下,只需要
kill 進程名<TAB>
,
zsh
就會自動補全進程的 pid。
其余我常用的補全還有:
-
ssh <TAB><TAB>時 zsh 會自動列出你訪問過的主機和用戶名來補全ssh的參數(shù)。 -
brew install <TAB><TAB>來補全軟件包名,除了 homebrew 以外,同樣支持 port/apt-get 等其他包管理器。
?
理由 4:zsh 支持更加聰明的目錄補全
以前比如想進入一個比較深的目錄,比如
/Users/pw/workspace/project/src/main/webapps/static/js
,就得在 bash 下面打半天,不停的 tab 去補全一個正確的路徑出來。在 zsh 下,只需要輸入每個路徑的頭字母然后 tab 一下:
cd /u/p/w/p/s/m/w/s/j<TAB>
?
理由 5:zsh 強大的快速目錄切換
以前最苦逼的事情莫過于頻繁在兩個工作目錄下切換,總要打一長串
cd
路徑。也嘗試過
popd
和
pushd
來解決這個問題,但往往是目錄已經(jīng)切換了才想起來沒用
pushd
。而 zsh 會記住你每一次切換的路徑,然后通過
1
來切換到你上一次訪問的路徑,
2
切換到上上次……一直到
9
,還可以通過
d
查看目錄訪問歷史。
zsh 還可以配合 autojump 一起使用,autojump 會記錄下每一個你訪問過的目錄,然后通過
j
來快速跳轉(zhuǎn)。
?
理由 6:zsh 支持全局 alias 和后綴名 alias
bash 的
alias
只能做命令的縮寫,而
zsh
更進一步,使
alias
可以縮寫命令的一部分,例如參數(shù)或環(huán)境變量設置。
$ alias -s log=less
$ ~/package/tomcat/log/catalina.log # 相當于 less ~/package/tomcat/log/catalina.log
$ alias -g PR=http_proxy=127.0.0.1:8087
$ PR curl https://twitter.com # 相當于 http_proxy=127.0.0.1:8087 curl https://twitter.com
?
理由 7:zsh 有著豐富多彩的命令行提示符
bash 下通過設置
$PS1
已經(jīng)可以實現(xiàn)很豐富的提示符了,而 zsh 更進一步,可以實現(xiàn)諸如多行提示符、提示符右對齊等功能。
oh-my-zsh
配置文件中提供了非常豐富的提示符 theme 供選擇,我使用的是
gentoo
主題,比較簡潔,還可以顯示當前 git 倉庫的狀態(tài)。
?
理由 8:zsh 有更多優(yōu)雅的語法
例如修改
PATH
,bash 下設置
$PATH
要求所有路徑都要寫在一行里,目錄多了以后看起來就很難看。zsh 支持更加符合程序員審美觀的設置方式。
path=(
~/bin
$path
~/package/smartsprites/bin
)
?
安裝 zsh
Linux 用戶通過各自發(fā)行版的包管理器直接安裝即可。
Mac 自帶一個 4.x.x 版本的 zsh,可以直接使用,也可以通過 homebrew 安裝最近剛剛發(fā)布的 5.0.0 版本。推薦使用最新的 5.0 版本,對多字節(jié)字符提供了完整的支持,這一點對于國內(nèi)用戶來說很重要。 詳細的 release note
?
設置為默認 shell
通過命令
chsh
修改默認登錄 shell,需要注意的是,如果通過 homebrew 安裝了最新版本的 zsh,則需要
sudo
編輯
/etc/shells
加入一行
/usr/local/bin/zsh
。然后再通過
chsh
來修改默認 shell,否則會提示
/usr/local/bin/zsh
不是合法的 shell。
?
安裝 oh-my-zsh 配置
對于每一個像我這樣的 zsh 初級用戶來說,oh-my-zsh 就是救人于水火中的大殺器,強烈建議使用此配置上手 zsh。
作者提供了傻瓜安裝命令:
curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh
也可以手工安裝, 具體步驟 。
?
幾個必備的插件
autojump
幫助快速目錄跳轉(zhuǎn)的小工具。首先要安裝 autojump,然后在
.zshrc
中開啟 autojump 插件。它會記錄下來每個你進入過的目錄,隨后通過
j 目錄名稱的一部分
就可快速跳轉(zhuǎn)到該目錄。
Youtube 視頻介紹
git
Git 命令補全,除了可以補全 git 的子命令、命令開關等常規(guī)補全項以外,還可以補全分支名等內(nèi)容,用 git 必開的插件。
osx
提供一些與 Mac OSX 系統(tǒng)交互的命令,比如:
-
man-preview 通過 preview 程序查看一個命令的手冊,例如
man-preview git - quick-look 快速預覽文件
- pfd 返回當前 finder 打開的文件夾的路徑
- cdf 切換到當前 finder 所在的目錄
?
由于一個莫名其妙的原因,突然想把系統(tǒng)的shell從bash換成終極shell——zsh,于是呢,就尋找了配置文件一份。
?
安裝zsh:
sudo apt-get install zsh
?
配置文件(長長長,出自《開源世界旅行指南》,注釋良好,茶葉修改過,請自行注釋掉你不需要的功能):
#命令提示符 {{{
RPROMPT=$(echo '%{\033[31m%}%D %T%{\033[m%}')
PROMPT=$(echo '%{\033[34m%}%M%{\033[32m%}%/
%{\033[36m%}%n %{\033[01;31m%}>%{\033[33m%}>%{\033[34m%}>%{\033[m%} ')
#}}}
#標題欄、任務欄樣式{{{
case $TERM in (*xterm*|*rxvt*|(dt|k|E)term)
precmd () { print -Pn "\e]0;%n@%M//%/\a" }
preexec () { print -Pn "\e]0;%n@%M//%/\ $1\a" }
;;
esac
#}}}
#關于歷史紀錄的配置 {{{
#歷史紀錄條目數(shù)量
export HISTSIZE=1000
#注銷后保存的歷史紀錄條目數(shù)量
export SAVEHIST=1000
#歷史紀錄文件
#export HISTFILE=~/.zhistory
#以附加的方式寫入歷史紀錄
setopt INC_APPEND_HISTORY
#如果連續(xù)輸入的命令相同,歷史紀錄中只保留一個
#setopt HIST_IGNORE_DUPS
#為歷史紀錄中的命令添加時間戳
setopt EXTENDED_HISTORY
#啟用 cd 命令的歷史紀錄,cd -[TAB]進入歷史路徑
setopt AUTO_PUSHD
#相同的歷史路徑只保留一個
#asetopt PUSHD_IGNORE_DUPS
#在命令前添加空格,不將此命令添加到紀錄文件中
#setopt HIST_IGNORE_SPACE
#}}}
#每個目錄使用獨立的歷史紀錄{{{
cd() {
builtin cd "$@" # do actual cd
fc -W # write current history file
local HISTDIR="$HOME/.zsh_history$PWD" # use nested folders for history
if [ ! -d "$HISTDIR" ] ; then # create folder if needed
mkdir -p "$HISTDIR"
fi
export HISTFILE="$HISTDIR/zhistory" # set new history file
touch $HISTFILE
local ohistsize=$HISTSIZE
HISTSIZE=0 # Discard previous dir's history
HISTSIZE=$ohistsize # Prepare for new dir's history
fc -R #read from current histfile
}
mkdir -p $HOME/.zsh_history$PWD
export HISTFILE="$HOME/.zsh_history$PWD/zhistory"
function allhistory { cat $(find $HOME/.zsh_history -name zhistory) }
function convhistory {
sort $1 | uniq |
sed 's/^:\([ 0-9]*\):[0-9]*;\(.*\)/\1::::::\2/' |
awk -F"::::::" '{ $1=strftime("%Y-%m-%d %T",$1) "|"; print }'
}
#使用 histall 命令查看全部歷史紀錄
function histall { convhistory =(allhistory) |
sed '/^.\{20\} *cd/i\\' }
#使用 hist 查看當前目錄歷史紀錄
function hist { convhistory $HISTFILE }
#全部歷史紀錄 top44
function top44 { allhistory | awk -F':[ 0-9]*:[0-9]*;' '{ $1="" ; print }' | sed 's/ /\n/g' | sed '/^$/d' | sort | uniq -c | sort -nr | head -n 44 }
#}}}
#a雜項 {{{
#允許在交互模式中使用注釋 例如:
#cmd #這是注釋
setopt INTERACTIVE_COMMENTS
#啟用自動 cd,輸入目錄名回車進入目錄
#稍微有點混亂,不如 cd 補全實用
#setopt AUTO_CD
#擴展路徑
#/v/c/p/p => /var/cache/pacman/pkg
setopt complete_in_word
#禁用 core dumps
limit coredumpsize 0
#Emacs風格 鍵綁定
bindkey -e
#設置 [DEL]鍵 為向后刪除
bindkey "\e[3~" delete-char
#以下字符視為單詞的一部分
#WORDCHARS='*?_-[]~=&;!#$%^(){}<>'
##LEO注釋了這部分
#}}}
#自動補全功能 {{{
setopt AUTO_LIST
setopt AUTO_MENU
#開啟此選項,補全時會直接選中菜單項
#setopt MENU_COMPLETE
autoload -U compinit
compinit
#自動補全緩存
#zstyle ':completion::complete:*' use-cache on
#zstyle ':completion::complete:*' cache-path .zcache
#zstyle ':completion:*:cd:*' ignore-parents parent pwd
#自動補全選項
zstyle ':completion:*:match:*' original only
zstyle ':completion::prefix-1:*' completer _complete
zstyle ':completion:predict:*' completer _complete
zstyle ':completion:incremental:*' completer _complete _correct
zstyle ':completion:*' completer _complete _prefix _correct _prefix _match _approximate
#路徑補全
zstyle ':completion:*' expand 'yes'
zstyle ':completion:*' squeeze-shlashes 'yes'
zstyle ':completion::complete:*' '\\'
zstyle ':completion:*' menu select
zstyle ':completion:*:*:default' force-list always
#彩色補全菜單
eval $(dircolors -b)
export ZLSCOLORS="${LS_COLORS}"
zmodload zsh/complist
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
#修正大小寫
zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}'
#錯誤校正
zstyle ':completion:*' completer _complete _match _approximate
zstyle ':completion:*:match:*' original only
zstyle ':completion:*:approximate:*' max-errors 1 numeric
#kill 命令補全
compdef pkill=kill
compdef pkill=killall
zstyle ':completion:*:*:kill:*' menu yes select
zstyle ':completion:*:*:*:*:processes' force-list always
zstyle ':completion:*:processes' command 'ps -au$USER'
#補全類型提示分組
zstyle ':completion:*:matches' group 'yes'
zstyle ':completion:*' group-name ''
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*:options' auto-description '%d'
zstyle ':completion:*:descriptions' format $'\e[01;33m -- %d --\e[0m'
zstyle ':completion:*:messages' format $'\e[01;35m -- %d --\e[0m'
zstyle ':completion:*:warnings' format $'\e[01;31m -- No Matches Found --\e[0m'
#}}}
##行編輯高亮模式 {{{
# Ctrl+@ 設置標記,標記和光標點之間為 region
zle_highlight=(region:bg=magenta #選中區(qū)域
special:bold #特殊字符
isearch:underline)#搜索時使用的關鍵字
#}}}
##空行(光標在行首)補全 "cd " {{{
user-complete(){
case $BUFFER in
"" ) # 空行填入 "cd "
BUFFER="cd "
zle end-of-line
zle expand-or-complete
;;
"cd --" ) # "cd --" 替換為 "cd +"
BUFFER="cd +"
zle end-of-line
zle expand-or-complete
;;
"cd +-" ) # "cd +-" 替換為 "cd -"
BUFFER="cd -"
zle end-of-line
zle expand-or-complete
;;
* )
zle expand-or-complete
;;
esac
}
zle -N user-complete
bindkey "\t" user-complete
#}}}
##在命令前插入 sudo {{{
#定義功能
#sudo-command-line() {
# [[ -z $BUFFER ]] && zle up-history
# [[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER"
# zle end-of-line #光標移動到行末
#}
#zle -N sudo-command-line
#定義快捷鍵為: [Esc] [Esc]
#bindkey "\e\e" sudo-command-line
#}}}
#命令別名 {{{
#alias -g cp='cp -i'
#alias -g mv='mv -i'
#alias -g rm='rm -i'
alias -g ls='ls -X -F --color=auto'
alias -g ll='ls -l'
alias -g grep='grep --color=auto'
alias -g la='ls -a'
#alias -g ee='emacsclient -t'
#alias -g tk='tmux kill-server'
#哈哈哈,我現(xiàn)在有了byobu
alias -g bb='byobu-tmux'
alias -g x='exit'
alias -g t='faweibo -t'
alias -g tm='tmux'
alias -g f='cat ~/日程'
#還是算了吧
#alias -g gcc='gcc -Wall'
#alias -g cd....='cd ../..'
#Well this must consult Gods later.__Leo
#[Esc][h] man 當前命令時,顯示簡短說明
alias run-help >&/dev/null && unalias run-help
autoload run-help
#歷史命令 top10
alias top10='print -l ${(o)history%% *} | uniq -c | sort -nr | head -n 10'
#}}}
#路徑別名 {{{
#進入相應的路徑時只要 cd ~xxx
#hash -d WWW="/home/lighttpd/html"
#hash -d ARCH="/mnt/arch"
#hash -d PKG="/var/cache/pacman/pkg"
#hash -d E="/etc/env.d"
#hash -d C="/etc/conf.d"
#hash -d I="/etc/rc.d"
#hash -d X="/etc/X11"
#hash -d BK="/home/r00t/config_bak"
hash -d lc="/home/leo/dev/learnC"
hash -d di="/home/leo/文檔/日記"
#}}}
##afor Emacs {{{
#在 Emacs終端 中使用 Zsh 的一些設置 不推薦在 Emacs 中使用它
if [[ "$TERM" == "dumb" ]]; then
setopt No_zle
PROMPT='%n@%M %/
>>'
alias ls='ls -F'
fi
#}}}
#{{{自定義補全
#補全 ping
zstyle ':completion:*:ping:*' hosts 192.168.128.1{38,} www.g.cn \
192.168.{1,0}.1{{7..9},}
#補全 ssh scp sftp 等
#my_accounts=(
#{r00t,root}@{192.168.1.1,192.168.0.1}
#kardinal@linuxtoy.org
#123@211.148.131.7
#)
#zstyle ':completion:*:my-accounts' users-hosts $my_accounts
#}}}
####{{{
function calc { echo $(($@)) }
function timeconv { date -d @$1 +"%Y-%m-%d %T" }
# }}}
## END OF FILE #################################################################
# vim:filetype=zsh foldmethod=marker autoindent expandtab shiftwidth=4
?
下面介紹Byobu
?
安裝一個:
sudo apt-get install byobu
?
簡單配置下:
~/.byobu/keybindings.tmux
#綁定一個F12切換
set -g prefix F12
unbind-key -n C-a
bind a send-prefix
unbind '"'
bind - splitw -v
unbind %
#bind | splitw -h
bind \ splitw -h
bind k selectp -U
bind j selectp -D
bind h selectp -L
bind l selectp -R
?
按下F12,然后再按下hjkl就可以切換格子了。
?
按下F2打開新窗口,F(xiàn)3、F4負責左右切換窗口。
我就用到這些功能了。
?
來源:http://leosong.diandian.com/post/2012-07-13/40030151442
?
在 Ubuntu Linux 中安裝 Zsh 及 Oh-my-zsh
直接用 apt-get 安裝 zsh 套件
$ apt-get install zsh
$ zsh --verison
zsh 4.3.17
?
從 GitHub 下載 oh-my-zsh 套件
$ git clone https://github.com/robbyrussell/oh-my-zsh.git ~/.oh-my-zsh
?
如果本來沒有安裝 zsh 可以直接使用 oh-my-zsh 的範例 zshrc
$ cp ~/.oh-my-zsh/templates/zshrc.zsh-template ~/.zshrc
?
看看有什麼 Theme 可以用
$ ls ~/.oh-my-zsh/themes
?
編輯 ~/.zshrc 更換 zsh 的 theme 我自己喜歡用 candy
ZSH_THEME="candy"
?
看看有什麼 Plugin 可以用
$ ls ~/.oh-my-zsh/plugins
?
編輯 ~/.zshrc 啟用 Plugin
plugins=(git git-flow debian grails rvm history-substring-search github gradle svn node npm zsh-syntax-highlighting sublime)
?
下載 zsh-syntax-highlighting plugin
$ cd ~/.oh-my-zsh/custom/plugins
$ git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
?
新增自訂 zsh 設定,我會把 alias 和 PATH 的設定放在這邊
$ cat ~/.oh-my-zsh/custom/xxx.zsh
alias df='df -h'
alias h='htop'
PATH=$PATH:/opt/app/bin/
?
來源:http://shyuan.github.io/blog/2012/07/10/install-zsh-and-oh-my-zsh-in-ubuntu-linux/
?
Make?
ZSH
?default
chsh -s /bin/zsh
?
Restart your system.
?
關聯(lián): https://github.com/robbyrussell/oh-my-zsh
?
更多文章、技術(shù)交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

