LoginSignup
2
1

More than 3 years have passed since last update.

zsh導入方法(.zshrcカスタマイズ付き)

Posted at

長らくfishを使用していたのですが、気分転換にこの度zshに乗り換えました!
今回は、zshの導入方法.zshrcによるzshのカスタマイズに関して記事にしていきます。

こちらの記事の設定でできるようになること

  • zshの強力な補完機能
  • fishのような以下のシンタックスハイライト
    • 文字入力
    • ls時のディレクトリ・ファイル
  • fishのような入力予測機能
  • gitのブランチ名のシンタックスハイライト
  • cd でディレクトリ移動時に自動で ls を実行

zshの導入方法

brewコマンドを使用してzshと今回使用するプラグインをダウンロードしていきます。

以下のコマンドを実行します。

$ brew install zsh

$ brew install zsh-completions
=> 補完機能の強化プラグイン

$ brew install zsh-autosuggestions
=> 入力の補完時の予測を影で表示してくれるプラグイン

$ brew install zsh-syntax-highlighting
=> 入力をシンタックスハイライトしてくれるプラグイン

上記を実行後

$ zsh

でzshを起動すると

  • q
  • 0
  • 1

上記の3択で入力を聞かれるので 0 を選択しenter(.zshrcを自動生成してくれる)

ログインシェルを変更

$ sudo vi /etc/shells
Password:

上記を実行後、下記を追記して保存

/usr/local/bin/zsh

qiita.png

chsh -s /usr/local/bin/zsh

上記を実行し、ログインシェルを変更

.zshrcの編集

ここまでの設定でホームディレクトリに.zshrcが生成されているはずです。

vi .zshrc

上記を実行し以下をコピペして保存

#補間
autoload -U compinit
compinit
#文字コード
export LANG=ja_JP.UTF-8
#プロンプト
autoload -U colors
colors

#履歴
#履歴を保存するファイル指定
HISTFILE="$HOME/.zsh_history"
#履歴の件数
HISTSIZE=100000
SAVEHIST=100000
#重複した履歴を保存しない
setopt hist_ignore_dups
#履歴を共有する
setopt share_history
#先頭にスペースを入れると履歴に残さない
setopt hist_ignore_space
#履歴の検索
autoload 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 "^N" history-beginning-search-forward-end 
#cdの設定
#ディレクトリ名だけで移動する。
setopt auto_cd
#自動でpushdする
setopt auto_pushd
#pushdの履歴は残さない。
setopt pushd_ignore_dups
#ターミナルのタイトル
case "${TERM}" in
kterm*|xterm)
    precmd() {
        echo -ne "\033]0;${USER}@${HOST}\007"
    }
    ;;
esac 
# コマンドミスを修正
setopt correct
# 補完の選択を楽にする
zstyle ':completion:*' menu select
# カレントディレクトリに候補がない場合のみ cdpath 上のディレクトリを候補に出す
zstyle ':completion:*:cd:*' tag-order local-directories path-directories
#cd は親ディレクトリからカレントディレクトリを選択しないので表示させないようにする (例: cd ../<TAB>):
zstyle ':completion:*:cd:*' ignore-parents parent pwd
# 補完候補をできるだけ詰めて表示する
setopt list_packed
#色の設定
export LSCOLORS=Exfxcxdxbxegedabagacad
# 補完時の色設定
export LS_COLORS='di=01;34:ln=01;35:so=01;32:ex=01;31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30'
autoload -U colors ; colors ; zstyle ':completion:*' list-colors "${LS_COLORS}"

#alias
case "${OSTYPE}" in
freebsd*|darwin*)
  alias ls="ls -GF"
  ;;
linux*)
  alias ls="ls -F --color"
  ;;
esac
#w3mでALC検索
function alc() {
  if [ $# != 0 ]; then
    w3m "http://eow.alc.co.jp/$*/UTF-8/?ref=sa"
  else
    w3m "http://www.alc.co.jp/"
  fi
}
#cdを打ったら自動的にlsを打ってくれる関数
function cd(){
    builtin cd $@ && ls;
}
#その他
#キーバインド
bindkey -e
#ビープ音ならなさない
setopt nobeep
#エディタ
export EDITOR=emacs
#改行のない出力をプロンプトで上書きするのを防ぐ
unsetopt promptcr
#個別設定を読み込む
[ -f ~/.zshrc.mine ] && source ~/.zshrc.mine
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh
# git設定
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]'
# -----------------------------
# Prompt
# -----------------------------
# %M    ホスト名
# %m    ホスト名
# %d    カレントディレクトリ(フルパス)
# %~    カレントディレクトリ(フルパス2)
# %C    カレントディレクトリ(相対パス)
# %c    カレントディレクトリ(相対パス)
# %n    ユーザ名
# %#    ユーザ種別
# %?    直前のコマンドの戻り値
# %D    日付(yy-mm-dd)
# %W    日付(yy/mm/dd)
# %w    日付(day dd)
# %*    時間(hh:flag_mm:ss)
# %T    時間(hh:mm)
# %t    時間(hh:mm(am/pm))
# PROMPT='[%n][%c]'\$vcs_info_msg_0_' $ '
PROMPT='%B%F{32}~/%C%f'\$vcs_info_msg_0_' $ '
#'di=01;34:ln=01;35:so=01;32:ex=01;31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30'
precmd(){ vcs_info }

上記保存後、

source .zshrc

で設定を反映させる。

補足

.zshrcを編集することで、自分好みにzshをカスタマイズできます。

※ シンタックスハイライトの色や、aliasの設定等もいじるともっと使いやすいかもしれません。

是非いろいろ試してみてください。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1