基礎環境
在進行高效的SHELL實踐之前,首先配置一下基礎環境,當然首先是需要一臺MacOS電腦。這裏採用: zsh
+ oh-my-zsh
+ zsh-completions
+ zsh-autosuggestions
。具體安裝步驟如下:
# 切換默認SHELL爲 zsh
$: chsh -s /bin/zsh
# 安裝 oh-my-zsh
$: sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
# 安裝 zsh-completions
$: git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-completions
# 安裝 zsh-autosuggestions
$: git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
安裝完成後,輕鬆敲打幾個命令,該提示的、補全的也都如預期般的展示,的確大大的提升了命令輸入的效率。
命令別名
減少輸入的另一個辦法就是對拼寫複雜的命令設置簡易的別名。alias
別名命令最常規的用法就是,定義別名。當然這是alias
命令的主要功能之一。不過它還具有其它功能,不細看的話很容易被忽略掉。
不妨通過tldr
命令查詢看看alias
的功能列表:
$: tldr alias
alias
Creates aliases -- words that are replaced by a command string.
Aliases expire with the current shell session, unless they're defined in the shell's configuration file, e.g. `~/.bashrc`.
- List all aliases:
alias
- Create a generic alias:
alias word="command"
- View the command associated to a given alias:
alias word
- Remove an aliased command:
unalias word
- Turn `rm` into an interactive command:
alias rm="rm -i"
- Create `la` as a shortcut for `ls -a`:
alias la="ls -a"
不難看出,alias
命令還有另外檢索的功能,該功能在我們設置別名時先判斷是否已經存在別名非常有用。
框架擴展
如果僅僅認爲oh-my-zsh只是提供的個性化的主題腳本框架,真是太小看它了。它一套真正的基於zsh
的腳本框架,其真正的威力還表現在其提供的200多個插件上, 當然這些插件是需要安裝的,在 oh-my-zsh
的插件目錄中僅僅是這些工具的輔助函數或是別名。通過這個插件目錄,我們可以發現大量功能強大的工具。當然我們也可以將自己常用的腳本放進來,作爲獨立的分支維護個人命令。
通常,oh-my-zsh
都會開啓默認插件git
功能。但是具體git
插件提供了哪些功能則需要通過插件的README文件。打開一看,裏面提供的別名有 141 個之多,這麼多的別名很明顯是無法記憶的。
# 統計一些git的別名總數
$: alias | grep ^g | wc -l
141
如果能夠在使用時快速的查詢這些別名,用時查詢,一旦用得多了,也就記住了。先通過別名命令手動查詢:
$: alias | grep ^g
g=git
ga='git add'
gaa='git add --all'
gap='git apply'
gapa='git add --patch'
gau='git add --update'
gav='git add --verbose'
gb='git branch'
gbD='git branch -D'
...
現在,我們就可以通過將這個簡單命令行,寫出自己的腳本,集成的oh-my-zsh
的框架,作爲自己的插件獨立維護。在oh-my-zsh
的插件目錄中,增加一個自定義的插件alias
.提供一個快速查詢現有別名的功能。
$: mkdir ~/.oh-my-zsh/plugins/alias
$: cd ~/.oh-my-zsh/plugins/alias
$: cat <<EOF > alias.plugin.zsh
function alias-find(){
alias | grep $1
}
alias af="alias-find "
EOF
或者定義一個匿名函數, 別名的另一種寫法。
$: alias af='f(){ alias | grep $@; unset -f f; }; f'
完成編輯後,.zshrc
中增加alias
插件。重新開啓新的SHELL窗口,現在就可以通過af
別名命令查詢已有的別名了。
$: af commit
gc='git commit -v'
'gc!'='git commit -v --amend'
gca='git commit -v -a'
'gca!'='git commit -v -a --amend'
gcam='git commit -a -m'
'gcan!'='git commit -v -a --no-edit --amend'
'gcans!'='git commit -v -a -s --no-edit --amend'
gcmsg='git commit -m'
'gcn!'='git commit -v --no-edit --amend'
gcs='git commit -S'
gcsm='git commit -s -m'
gdt='git diff-tree --no-commit-id --name-only -r'
git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk'
gsd='git svn dcommit'
gwch='git whatchanged -p --abbrev-commit --pretty=medium'
gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign -m "--wip-- [skip ci]"'
這樣就可以快速的查詢已有存在commit
內容的別名命令了。
總結
-
oh-my-zsh
是一個腳本框架,如果使用該框架,儘可能最大化的使用到它的功能。 - 擴展自己的插件,可以非常有效的管理自己的腳本。
參考項目
個人的alias
插件項目,請參閱:liujianping/oh-my-zsh.