oh-my-zsh 設定


Outline
  1. 1. Shell? bash v.s. zsh
  2. 2. Configuration Environment
  3. 3. Install oh-my-zsh
    1. 3.1. Step 1: github 下載 oh-my-zsh
    2. 3.2. Step 2: 設定 PATH,railscasts Oh My ZSH
    3. 3.3. Step 3: 設定 zsh vi ~/.zshrc,下面是我目前的設定檔範例
    4. 3.4. Step 4: mac iterm2 設定 default zsh
  4. 4. 自定義主題
  5. 5. Plugin

前陣子介紹了 漂漂終端機懶人包,把終端機變得美美的方法,但是其實有一個更強大的 framework 叫做 oh-my-zsh,它是一個大家可以一起編輯的 zsh framework,擁有 120+ plugins、120+ themes,所以可以跟大家分享自己的主題,或是拿別人的來用。除了主題之外,他的 plugin 強大的功能也是令大眾喜愛的原因,譬如你想要 簡寫偷懶 的做某件事,如果不想要要靠 batch 檔來執行,可以用 oh-my-zsh 的 custom plugin,然後可以分享給其他人。

你應該開始使用 “OH MY ZSHELL!”

▲ oh-my-zsh

Shell? bash v.s. zsh

至於兩個 Shell 差別在哪?來看看官方 FAQ 說法:

2.5: Similarities with bash

The Bourne-Again Shell, bash, is another enhanced Bourne-like shell; the most obvious difference from zsh is that it does not attempt to emulate the Korn shell. Since both shells are under active development it is probably not sensible to be too specific here. Broadly, bash has paid more attention to standards compliancy (i.e. POSIX) for longer, and has so far avoided the more abstruse interactive features (programmable completion, etc.) that zsh has.

但我覺得差別最大的應該是 tab 鍵的應用。

Configuration Environment

  1. Unix-like system: OSX
  2. Linux: Ubuntu

Install oh-my-zsh

Step 1: github 下載 oh-my-zsh

1
curl -L https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh | sh

Step 2: 設定 PATH,railscasts Oh My ZSH

1
cat ~/.bash_profile >> ~/.zshrc

Step 3: 設定 zsh vi ~/.zshrc,下面是我目前的設定檔範例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Path to your oh-my-zsh configuration.
ZSH=$HOME/.oh-my-zsh
# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Optionally, if you set this to "random", it'll load a random theme each
# time that oh-my-zsh is loaded.
ZSH_THEME="kennethreitz"
#ZSH_THEME="eastwood"
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
# Set to this to use case-sensitive completion
# CASE_SENSITIVE="true"
# Comment this out to disable bi-weekly auto-update checks
# DISABLE_AUTO_UPDATE="true"
DISABLE_UPDATE_PROMPT="true"
# Uncomment to change how often before auto-updates occur? (in days)
# export UPDATE_ZSH_DAYS=13
# Uncomment following line if you want to disable colors in ls
DISABLE_LS_COLORS="true"
# Uncomment following line if you want to disable autosetting terminal title.
# DISABLE_AUTO_TITLE="true"
# Uncomment following line if you want to disable command autocorrection
DISABLE_CORRECTION="true"
# Uncomment following line if you want red dots to be displayed while waiting for completion
# COMPLETION_WAITING_DOTS="true"
# Uncomment following line if you want to disable marking untracked files under
# VCS as dirty. This makes repository status check for large repositories much,
# much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
plugins=(sublime)
source $ZSH/oh-my-zsh.sh
# Customize to your needs...
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
# Load RVM into a shell session *as a function*

Step 4: mac iterm2 設定 default zsh

▲ mac iterm2

自定義主題

主題除了在 github wiki 上挑選外,還可以查看圖示列表 http://zshthem.es/all/。而我最後挑選了 kennethreitz 作為我的主題,並且做了一些更動,以下是我的主題設定檔:

1
vim ~/.oh-my-zsh/themes/kennethreitz.zsh-theme
1
2
3
4
5
6
7
8
9
10
11
12
local return_code="%(?..%{$fg[red]%}%? ↵%{$reset_color%})"
PROMPT='
[%{$fg[blue]%}%~%{$reset_color%} ${return_code}]
%{$fg[green]%}%(!.#.$)%{$reset_color%} '
PROMPT2='%{$fg[red]%}\ %{$reset_color%}'
RPS1='$(git_prompt_info)'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$reset_color%}%{$fg[yellow]%}["
ZSH_THEME_GIT_PROMPT_SUFFIX="]%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_CLEAN=""
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}*%{$fg[yellow]%}"

▲ Self-config theme

Plugin

sublime.plugin.zsh

舉一個例子我有 plugins=(sublime) ,然後來看看他的 zsh code 是怎麼寫的,然後讓我們加入幾行 code 使得可以使用 st3 來開啟最新版本的 sublime 3:

1
vim ~/.oh-my-zsh/plugins/sublime/sublime.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Sublime Text 2 Aliases
local _sublime_darwin_paths > /dev/null 2>&1
_sublime_darwin_paths=(
"/usr/local/bin/subl"
"$HOME/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"$HOME/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
"$HOME/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
"/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl"
"/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl"
"/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl"
)
if [[ $('uname') == 'Linux' ]]; then
if [ -f '/usr/bin/sublime_text' ]; then
st_run() { nohup /usr/bin/sublime_text $@ > /dev/null & }
else
st_run() { nohup /usr/bin/sublime-text $@ > /dev/null & }
fi
alias st=st_run
elif [[ $('uname') == 'Darwin' ]]; then
for _sublime_path in $_sublime_darwin_paths; do
if [[ -a $_sublime_path ]]; then
alias subl="'$_sublime_path'"
alias st=subl
break
fi
done
fi
alias stt='st .'
local _sublime_darwin_subl3=/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl
if [[ $('uname') == 'Darwin' ]]; then
# Check if Sublime is installed in user's home application directory
if [[ -a $HOME/${_sublime_darwin_subl3} ]]; then
alias st3='$HOME/${_sublime_darwin_subl3}'
else
alias st3='${_sublime_darwin_subl3}'
fi
fi
alias stt3='st3 .'

如此一來就可以透過指令來啓動 sublime囉,超級方便。

1
2
3
$ st .
# or
$ st3 .