インストール - Zsh
ナビゲーションに移動
検索に移動
概要
Zshはシェルの一種で、Linux等の標準シェルであるbashよりも便利な機能を持ったシェルである。
Linuxにてターミナルでコマンドを実行することが多々あるが、zshに切り替えることで、作業効率を上げることができる。
ここでは、Zshの最新版をインストールする方法を記載する。
なお、CentOSのパッケージ管理システムでは、最新のZshをインストール出来ないため、yumは使用しない。
最新のZshはこちらのWebサイトで確認できる。
Zshのインストール
CentOSの場合
Zshのインストールには、ncurses-develが必要となるので、以下のコマンドを実行してインストールする。
sudo yum install ncurses-devel
続いて、Zshのソースコードをダウンロードする。(Zshのソースコードをダウンロードするディレクトリは任意の場所で構わない)
また、--enable-multibyteオプションは、マルチバイトを有効にするオプションになる。
wget https://sourceforge.net/projects/zsh/files/zsh/5.5.1/zsh-5.5.1.tar.xz/download -O zsh-5.8.tar.xz tar xvf zsh-5.5.1.tar.xz -C /usr/local/src cd /usr/local/src/zsh-5.5.1 sudo ./configure --enable-multibyte sudo make && sudo make install
Zshを利用可能にする
Zshを利用可能なシェル一覧に追加する。
ここに追加することで、chshコマンドでシェルの変更が可能となる。
sudo echo /usr/local/bin/zsh >> /etc/shells
次に、ユーザのシェルを変更する。(コマンドを実行するとパスワードの入力が必要となる)
chsh -s /usr/local/bin/zsh
再度ログインするか再起動を行う。
すると、以下の文言が表示される。これは、Zshに設定ファイルが存在しないために表示される。
ここでは、 0を入力して空の設定ファイルを作成する。
This is the Z Shell configuration function for new users, zsh-newuser-install.
You are seeing this message because you have no zsh startup files(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory~).
This function can help you with a few settings that should make your use of the shell easier.
You can:
(q) Quit and do nothing. The function will be run again next time.
(0) Exit, creating the file ~/.zshrc containing just a comment.
That will prevent this function being run again.
(1) Continue to the main menu.
--- Type one of the keys in parentheses ---
Zshの設定例(/home/ユーザ名/.zshrcファイル)
# Ctrl + Dでログアウトしてしまうことを防ぐ
setopt IGNOREEOF
# 日本語を使用
export LANG=ja_JP.UTF-8
# パスを追加したい場合
export PATH="$HOME/bin:$PATH"
# 色を使用
autoload -Uz colors
colors
# 補完
autoload -Uz compinit
compinit
# emacsキーバインド
bindkey -e
# 他のターミナルとヒストリーを共有
setopt share_history
# ヒストリーに重複を表示しない
setopt histignorealldups
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
# cdコマンドを省略して、ディレクトリ名のみの入力で移動
setopt auto_cd
# 自動でpushdを実行
setopt auto_pushd
# pushdから重複を削除
setopt pushd_ignore_dups
# コマンドミスを修正
setopt correct
# グローバルエイリアス
alias -g L='| less'
alias -g H='| head'
alias -g G='| grep'
alias -g GI='| grep -ri'
# エイリアス
# backspace,deleteキーを使えるように
stty erase ^H
bindkey "^[[3~" delete-char
# cdの後にlsを実行
chpwd() { ls -ltr --color=auto }
# どこからでも参照できるディレクトリパス
cdpath=(~)
# 区切り文字の設定
autoload -Uz select-word-style
select-word-style default
zstyle ':zle:*' word-chars "_-./;@"
zstyle ':zle:*' word-style unspecified
# Ctrl + sのロック, Ctrl + qのロック解除を無効にする
setopt no_flow_control
# プロンプトを2行で表示、時刻を表示
PROMPT="%(?.%{${fg[green]}%}.%{${fg[red]}%})%n${reset_color}@${fg[blue]}%m${reset_color}(%*%) %~%# "
# 補完後、メニュー選択モードになり左右キーで移動が出来る
zstyle ':completion:*:default' menu select=2
# 補完で大文字にもマッチ
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
# Ctrl+rでヒストリーのインクリメンタルサーチ、Ctrl+sで逆順
bindkey '^r' history-incremental-pattern-search-backward
bindkey '^s' history-incremental-pattern-search-forward
# コマンドを途中まで入力後、historyから絞り込み
# 例 ls まで打ってCtrl+pでlsコマンドをさかのぼる、Ctrl+bで逆順
autoload -Uz history-search-end
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
bindkey "^p" history-beginning-search-backward-end
bindkey "^b" history-beginning-search-forward-end
# cdrコマンドを有効 ログアウトしても有効なディレクトリ履歴
# cdr タブでリストを表示
autoload -Uz add-zsh-hook
autoload -Uz chpwd_recent_dirs cdr
add-zsh-hook chpwd chpwd_recent_dirs
# cdrコマンドで履歴にないディレクトリにも移動可能に
zstyle ":chpwd:*" recent-dirs-default true
# 複数ファイルのmv 例 zmv *.txt *.txt.bk
autoload -Uz zmv
alias zmv='noglob zmv -W'
# mkdirとcdを同時実行
function mkcd() {
if -d $1 ; then
echo "$1 already exists!"
cd $1
else
mkdir -p $1 && cd $1
fi
}
# git設定
RPROMPT="%{${fg[blue]}%}[%~]%{${reset_color}%}"
autoload -Uz vcs_info
setopt prompt_subst
zstyle ':vcs_info:git:*' check-for-changes true
zstyle ':vcs_info:git:*' stagedstr "%F{yellow}!"
zstyle ':vcs_info:git:*' unstagedstr "%F{red}+"
zstyle ':vcs_info:*' formats "%F{green}%c%u[%b]%f"
zstyle ':vcs_info:*' actionformats '[%b|%a]'
precmd () { vcs_info }
RPROMPT=$RPROMPT'${vcs_info_msg_0_}'