Oh My Zshの処理を見てみる(1): zshrc.zsh-template
Oh My Zsh
Zshの設定フレームワーク、プラグインマネージャには以下のようなものがあり、中でもOh My Zshの人気が高い。
Oh My Zshは色々な設定が簡単にできて便利だが、使いこなせてなかったり不要な設定をしている場合があるので、今更ながらだが実際に何の処理をしているかを見てみる。
zshrc.zsh-template
Oh My Zshではtemplates/zshrc.zsh-templateというテンプレートファイルを~/.zshrc
にリネームして使用するので、まずはこのファイルの処理を見てみる。
# If you come from bash you might have to change your $PATH. # export PATH=$HOME/bin:/usr/local/bin:$PATH
PATH
に$HOME/bin
と/usr/local/bin
を追加する場合は有効化(コメントを外す)する。BashからZshにした場合はPATH
を変更する必要があるかもとのこと。
# Path to your oh-my-zsh installation. export ZSH=$HOME/.oh-my-zsh
ZSH
にOh My Zshをインストールしたディレクトリ($HOME/.oh-my-zsh
)を指定する。
# 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="robbyrussell"
ロードしたいテーマの名前を設定する。デフォルトで設定されているテーマはrobbyrussell
で、random
を指定すれば起動時にテーマがランダムで選択される。テーマについての詳細は下記記事を参照。
wonderwall.hatenablog.com
# Uncomment the following line to use case-sensitive completion. # CASE_SENSITIVE="true"
補完時に大文字と小文字を区別して補完したい場合は有効化する。
# Uncomment the following line to use hyphen-insensitive completion. Case # sensitive completion must be off. _ and - will be interchangeable. # HYPHEN_INSENSITIVE="true"
補完時に_
と-
を区別せずに補完したい場合は有効化する。ただし、CASE_SENSITIVEは無効化されている必要がある。
補完についてはlib/completion.zsh
に以下のような条件分岐で記述されているが、ややこしそうなので後回し。
if [ "x$CASE_SENSITIVE" = "xtrue" ]; then zstyle ':completion:*' matcher-list 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' unset CASE_SENSITIVE else if [ "x$HYPHEN_INSENSITIVE" = "xtrue" ]; then zstyle ':completion:*' matcher-list 'm:{a-zA-Z-_}={A-Za-z_-}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' unset HYPHEN_INSENSITIVE else zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*' fi fi
# Uncomment the following line to disable bi-weekly auto-update checks. # DISABLE_AUTO_UPDATE="true"
自動アップデートをしたくない場合は有効化する。
# Uncomment the following line to change how often to auto-update (in days). # export UPDATE_ZSH_DAYS=13
自動アップデートの更新間隔を設定する。tools/check_for_upgrade.sh
を見るとデフォルトの値は13となっているので、2週間毎に自動アップデートされる。
# Uncomment the following line to disable colors in ls. # DISABLE_LS_COLORS="true"
ls
コマンド実行時に色分けしたくない場合は有効化する。
# Uncomment the following line to disable auto-setting terminal title. # DISABLE_AUTO_TITLE="true"
ターミナルのタイトル表示を変えたくない場合は有効化する。
# Uncomment the following line to enable command auto-correction. # ENABLE_CORRECTION="true"
コマンドを間違えたときに訂正する機能を使いたい場合は有効化する。有効化すると以下のように訂正候補を表示してくれるようになる。nyae
はそれぞれno(訂正しないで実行)、yes(訂正して実行)、abort(実行中止)、edit(コマンドを編集)の略らしい。
$ sl zsh: correct 'sl' to 'ls' [nyae]?
lib/correction.zsh
で詳細が記述されており、訂正を無視したいコマンドにはnocorrect
が指定されている。
if [[ "$ENABLE_CORRECTION" == "true" ]]; then alias ebuild='nocorrect ebuild' alias gist='nocorrect gist' alias heroku='nocorrect heroku' alias hpodder='nocorrect hpodder' alias man='nocorrect man' alias mkdir='nocorrect mkdir' alias mv='nocorrect mv' alias mysql='nocorrect mysql' alias sudo='nocorrect sudo' setopt correct_all fi
# Uncomment the following line to display red dots whilst waiting for completion. # COMPLETION_WAITING_DOTS="true"
コマンドの補完待ち時に赤色のドット(......
)を表示したい場合は有効化する。
# Uncomment the 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"
テーマによってはgitのワーキングディレクトリがdirtyであるかを確認してプロンプトに表示するが、未追跡ファイルを無視したい場合は有効化する。大規模レポジトリでは処理が速くなるとのこと。
lib/git.zsh
には以下のように記述されておりgit status
に--untracked-files=no
オプションを追加する。
if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" == "true" ]]; then FLAGS+='--untracked-files=no' fi STATUS=$(command git status ${FLAGS} 2> /dev/null | tail -n1)
# Uncomment the following line if you want to change the command execution time # stamp shown in the history command output. # The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd" # HIST_STAMPS="mm/dd/yyyy"
以下のようにhistory
コマンドで実行日時情報を表示したい場合は有効化する。日時のフォーマットは上記3通りを指定できる。
4591 9/11/2016 16:19 cd 4592 9/11/2016 16:19 ls …
# Would you like to use another custom folder than $ZSH/custom? # ZSH_CUSTOM=/path/to/new-custom-folder
ユーザオリジナルの設定ファイルなどを置くカスタムディレクトリをデフォルトの$ZSH/custom
から変更したい場合は設定する。
# 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) # Add wisely, as too many plugins slow down shell startup. plugins=(git)
ロードしたいプラグインを半角スペース区切りで列挙する。ただし、プラグインを追加し過ぎると起動時間が遅くなる。
source $ZSH/oh-my-zsh.sh
メインのoh-my-zsh.shを読み込む。oh-my-zsh.shについては下記記事を参照。
wonderwall.hatenablog.com
# User configuration # export MANPATH="/usr/local/man:$MANPATH"
ここからユーザオリジナルの設定を行う。
man
コマンドの検索パスを追加する場合は設定する。なお、manpath
コマンドでパスを確認できる。
# You may need to manually set your language environment # export LANG=en_US.UTF-8
LANG
の設定。日本語ならexport LANG=ja_JP.UTF-8
。
# Preferred editor for local and remote sessions # if [[ -n $SSH_CONNECTION ]]; then # export EDITOR='vim' # else # export EDITOR='mvim' # fi
EDITOR
の設定。sshでログインした場合はSSH_CONNECTION
に値がセットされるので、それで条件分岐している。mvim
はMacVimのコマンド。
# Compilation flags # export ARCHFLAGS="-arch x86_64"
コンパイルフラグ。Macでコンパイルしてインストールするときに必要になる場合がある。
# ssh # export SSH_KEY_PATH="~/.ssh/dsa_id"
SSH_KEY_PATH
にsshの鍵のパスを設定。
# Set personal aliases, overriding those provided by oh-my-zsh libs, # plugins, and themes. Aliases can be placed here, though oh-my-zsh # users are encouraged to define aliases within the ZSH_CUSTOM folder. # For a full list of active aliases, run `alias`. # # Example aliases # alias zshconfig="mate ~/.zshrc" # alias ohmyzsh="mate ~/.oh-my-zsh"
エイリアスの設定。ここで設定すればOh My Zshが設定したエイリアスも上書きされる。ただし、コメントにも書いてあるようにZSH_CUSTOM
ディレクトリ内のファイルでエイリアスを設定する方がよいらしい。