on my zsh

MacOS 自帶的bash 作為幾乎所有Linux發(fā)行版的默認(rèn)終端,正常使用時(shí)沒什么問(wèn)題的

這里介紹一個(gè)更強(qiáng)大的終端神器

背景介紹

在unix 內(nèi)核的操作系統(tǒng)中,當(dāng)然現(xiàn)在衍生出好多分支,linux ,OS X 都算.

shell 就算和上面這些系統(tǒng)內(nèi)核指令打交道的一座橋梁,我們通過(guò)鍵盤輸入一種自己容易記憶識(shí)別的符號(hào)標(biāo)識(shí)(shell 命令)

然后 shell解析這種命令再反饋給內(nèi)核去執(zhí)行一系列操作.

zsh 和 shell 有什么關(guān)系呢?

其實(shí) zsh 也是一種 shell ,但是并不是我們系統(tǒng)默認(rèn)的 shell ,unix 衍生系統(tǒng)的默認(rèn)shell 都是 bash。

查看已安裝shell
查看Mac上已有的shell,一共有6種


cat /etc/shells

/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

安裝“oh my zsh”

使用 crul 安裝:

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

使用 wget 安裝:

sh -c "$(wget https://raw.githubusercontent.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.

安裝完成以后,默認(rèn)Shell~/.bashrc文件默認(rèn)不再加載了,替代的是~/.zlogin~/.zshrc。所以如果你在~/.bashrc里配置了某些設(shè)置,需要把她們復(fù)制到~/.zshrc中。

~/.zshrc 中添加以下行

source ~/.bash_profile

切換終端為zsh

chsh -s /bin/zsh

oh my zsh 目錄結(jié)構(gòu)

進(jìn)入~/.oh-my-zsh目錄后,看看該目錄的結(jié)構(gòu)

~ ls ~/.oh-my-zsh
CONTRIBUTING.md cache           log             templates
LICENSE.txt     custom          oh-my-zsh.sh    themes
README.md       lib             plugins         tools
  • lib 提供了核心功能的腳本庫(kù)
  • tools 提供安裝、升級(jí)等功能的快捷工具
  • plugins 自帶插件的存在放位置
  • templates 自帶模板的存在放位置
  • themes 自帶主題文件的存在放位置
  • custom 個(gè)性化配置目錄,自安裝的插件和主題可放這里

3.配置

zsh 的配置主要集中在~/.zshrc里,用 vim 或你喜歡的其他編輯器打開.zshrc。

可以在此處定義自己的環(huán)境變量和別名,當(dāng)然,oh my zsh 在安裝時(shí)已經(jīng)自動(dòng)讀取當(dāng)前的環(huán)境變量并進(jìn)行了設(shè)置,你可以繼續(xù)追加其他環(huán)境變量。

別名設(shè)置:

zsh不僅可以設(shè)置通用別名,還能針對(duì)文件類型設(shè)置對(duì)應(yīng)的打開程序,比如:

  • alias -s html=vi,意思就是你在命令行輸入 hello.html,zsh會(huì)為你自動(dòng)打開vim并讀取hello.html
  • alias -s gz='tar -xzvf',表示自動(dòng)解壓后綴為gz的壓縮包。
alias cls='clear'
alias ll='ls -l'
alias la='ls -a'
alias vi='vim'
alias javac="javac -J-Dfile.encoding=utf8"
alias grep="grep --color=auto"
alias -s html=vi   # 在命令行直接輸入后綴為 html 的文件名,會(huì)在 vim 中打開
alias -s rb=vi     # 在命令行直接輸入 ruby 文件,會(huì)在 vim 中打開
alias -s py=vi       # 在命令行直接輸入 python 文件,會(huì)用 vim 中打開,以下類似
alias -s js=vi
alias -s c=vi
alias -s java=vi
alias -s txt=vi
alias -s gz='tar -xzvf'
alias -s tgz='tar -xzvf'
alias -s zip='unzip'
alias -s bz2='tar -xjvf'

【其他】

主題設(shè)置:

oh my zsh 提供了數(shù)十種主題,相關(guān)文件在~/.oh-my-zsh/themes目錄下,你可以自己選擇,也可以自己編寫主題。

.zshrc里找到ZSH_THEME,就可以設(shè)置主題了,默認(rèn)主題是:ZSH_THEME=”robbyrussell”

ZSH_THEME="random",主題設(shè)置為隨機(jī),這樣我們每打開一個(gè)窗口,都會(huì)隨機(jī)在默認(rèn)主題中選擇一個(gè)。

插件設(shè)置:

oh my zsh項(xiàng)目提供了完善的插件體系,相關(guān)的文件在~/.oh-my-zsh/plugins目錄下,默認(rèn)提供了100多種,大家可以根據(jù)自己的實(shí)際學(xué)習(xí)和工作環(huán)境采用,想了解每個(gè)插件的功能,只要打開相關(guān)目錄下的 zsh 文件看一下就知道了。插件也是在.zshrc里配置,找到plugins關(guān)鍵字,你就可以加載自己的插件了,系統(tǒng)默認(rèn)加載git,你可以在后面追加內(nèi)容,如下:

plugins=(git zsh-autosuggestions autojump zsh-syntax-highlighting)
安裝 zsh-autosuggestions

autosuggestions它是Oh-myszh的一個(gè)插件,作用基本上是根據(jù)歷史輸入指令的記錄即時(shí)的提示,能夠很大的提高效率

git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions

添加至 plugins

安裝 zsh-syntax-highlighting

代碼高亮插件可以讓終端顏色更加絢麗

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

添加至 plugins

plugins=(zsh-autosuggestions git zsh-syntax-highlighting)

效果圖

卸載oh my zsh

直接在終端中,運(yùn)行uninstall_oh_my_zsh既可以卸載。

博客地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容