天天看點

Oh-My-Zsh的配置與使用

什麼是Shell?

相對于核心來說,Shell是Linux/Unix的一個外殼,它負責外界與Linux核心的互動,接收使用者或其他應用程式的指令,然後把這些指令轉化成核心能了解的語言,傳給核心,核心是真正幹活的,幹完之後再把結果傳回使用者或應用程式。

簡單的說,shell就是那“黑乎乎”的指令行。

Shell的分類

Linux/Unix提供了很多種Shell,不同的shell具備不同的功能,shell還決定了腳本中函數的文法,Linux中預設的shell是/bin/bash;

想知道你的系統有幾種shell,可以通過以下指令檢視:

cat /etc/shells
           

顯示如下:

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

bash這個是目前大多數Linux系統預設使用的shell,全名是BourneAgain Shell,一共有40個指令。包含的功能幾乎可以涵蓋shell所具有的功能,是以一般的shell腳本都會指定它為執行路徑。

在 Linux 裡執行這個指令和 Mac 略有不同,你會發現 Mac 多了一個 zsh,也就是說 OS X 系統預裝了個 zsh,它是什麼呢?

zsh介紹

zsh 是一款功能強大的 shell 軟體,它可以相容 bash,并且提供了很多高效的改進。它是Linux裡最龐大的一種shell,它有84個内部指令,也提供了更為強大的功能:

  • 更好的自動補全
  • 更好的檔案名展開
  • 豐富的插件
  • 強大的定制性

但是由于配置過于複雜,一般情況下,我們不會使用該shell,直到「oh my zsh」的出現。

zsh安裝

如果你用 Mac,就可以直接看下一節,Mac預設已經安裝;

如果你用 Redhat Linux,執行:sudo yum install zsh;

如果你用 Ubuntu Linux,執行:sudo apt-get install zsh;

oh my zsh

Oh My Zsh(http://ohmyz.sh/)是一款社群驅動的指令行工具,正如它的首頁上說的,Oh My Zsh 是一種生活方式。它基于zsh指令行,提供了主題配置,插件機制,已經内置的便捷操作。給我們一種全新的方式使用指令行。

Oh My Zsh隻是一個對zsh指令行環境的配置包裝架構,但它不提供指令行視窗,更不是一個獨立的APP。

安裝

官網推薦安裝方式:

Via curl:

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

Via wget:

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

切換系統shell:

$ chsh -s /bin/zsh
           

配置

zsh的配置檔案存在目前使用者目錄中的.zshrc檔案,如果你發現切換了shell之後,以前的配置的環境變量不生效了,可以打開 .zshrc檔案,找到:

# User configuration
 source ~/.bash_profile
           

指定配置的環境變量檔案,之後運作:

source .zshrc
           

主題設定

在.zshrc檔案中找到主題的配置項

# Set name of the theme to load. Optionally, if you set this to "random"
# it'll load a random theme each time that oh-my-zsh is loaded.
# See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="ys"
           

這裡可以設定主題的名字,那麼這些主題的名字從哪裡找呢?

進入Oh My Zsh的配置目錄中:

ls /Users/使用者/.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
....
           

或者我們将主題設定為随機('random'),每次打開指令行視窗,都會随機在預設主題中選擇一個,如果遇到你喜歡的主題,可以輸入指令檢視其名字:

$ echo $ZSH_THEME
           

插件開啟

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
           

我們打開.zshrc配置檔案,定位到plugins

plugins=(
   git )
           

可以看到預設隻開啟了git插件,我們可以将要使用的插件的名字以空格相隔接在後面就可以了,比如:

plugins=(
   git adb)
           

如果我們要下載下傳第三方的插件,隻需要把插件下載下傳存放到~/.oh-my-zsh/plugins中,然後在上面加上插件的名字即可;

推薦插件

zsh-autosuggestions

它是Oh-myszh的一個插件,作用基本上是根據曆史輸入指令的記錄即時的提示,能夠很大的提高效率。

1.克隆到插件目錄:

git clone git://github.com/zsh-users/zsh-autosuggestions
           

2.修改配置檔案.zshrc:

plugins=(git zsh-autosuggestions)
           

zsh-syntax-highlighting

這是一個指令高亮插件,輸入為綠色時表示可用指令,路徑帶有下劃線時表示可用路徑

1.克隆到插件目錄:

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
           

2.修改配置檔案.zshrc:

plugins=(git zsh-syntax-highlighting)
           

最後别忘了讓配置生效

source .zshrc
           

引用:

終極 Shell.

利用Oh-My-Zsh打造你的超級終端