LoginSignup
4
1

More than 1 year has passed since last update.

Prezto をやめてイチから .zshrc をカスタマイズした

Last updated at Posted at 2021-12-07

はじめに

PC 新調を機に、導入することで色々なオプションやエイリアスが設定されるがあまり使いこなせていなかった Prezto をやめてフレームワークを使わず、必要な機能だけに絞って .zshrc をカスタマイズしてみました。
その際に便利だと思った内容をピックアップしてご紹介します。

ディレクトリ移動関連

setopt auto_cd     # cd を省略してディレクトリを移動
setopt auto_pushd  # cd 時に直前のディレクトリをスタックに追加する スタックは cd -<TAB_KEY> で呼び出せる
DIRSTACKSIZE=50    # スタック数の上限

setopt pushd_ignore_dups  # 重複するディレクトリをスタックに追加しない
setopt cd_silent          # cd - や cd -<TAB_KEY> でディレクトリを移動した後にカレントディレクトリを出力しない

補完関連

# 補完を拡張する https://github.com/zsh-users/zsh-completions
if type brew &>/dev/null; then
  FPATH=$(brew --prefix)/share/zsh-completions:$FPATH
fi

autoload -Uz compinit && compinit -i  # 補完を有効にする

zstyle ':completion:*' menu select  # Tab で選択中の項目をハイライト

# 大文字小文字を無視し、部分一致でマッチングして補完する
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'

ヒストリ関連

HISTFILE=~/.zsh_history
HISTSIZE=10000  # メモリ上にキャッシュする上限
SAVEHIST=50000  # HISTFILE に書き込まれる上限

setopt append_history        # 複数のセッションで使用する時などにヒストリを置き換えないようにし、追加を行うようにする
# setopt inc_append_history  # 入力されるとすぐにヒストリに追加する share_history と排他的な設定
setopt share_history         # inc_append_history に加えて他のセッションで追加されたヒストリを利用できるようになる

setopt hist_ignore_all_dups  # ヒストリにあるコマンドと重複する場合に古いコマンドはリストから削除する
setopt hist_ignore_space     # コマンドラインの最初の文字がスペースである場合、ヒストリからコマンドを削除する

bindkey "^p" history-beginning-search-backward  # コマンド入力中に Control + p でヒストリから後方に補完候補を検索して補完する
bindkey "^n" history-beginning-search-forward   # コマンド入力中に Control + n でヒストリから前方に補完候補を検索して補完する

コマンド入力中のヒストリ検索補完は特によく使う
https://zsh.sourceforge.io/Doc/Release/Zsh-Line-Editor.html#:~:text=history%2Dbeginning%2Dsearch%2Dbackward

コマンド入力中の補完後にカーソル位置が末尾に移動しないのが不便に感じる場合はこちら

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

入出力関連

setopt correct  # スペル修正を提案する
setopt interactivecomments  # インタラクティブシェルでもコメントを許可する

プロンプト

# Git の情報を表示する
autoload -Uz vcs_info
precmd_vcs_info() { vcs_info }
precmd_functions+=( precmd_vcs_info )
setopt prompt_subst
zstyle ':vcs_info:git:*' check-for-changes true          # staged/unstaged の反映を有効にする
zstyle ':vcs_info:git:*' stagedstr '+'                   # staged がある場合に表示する %c に対応
zstyle ':vcs_info:git:*' unstagedstr '*'                 # unstaged がある場合に表示する %u に対応
zstyle ':vcs_info:git:*' formats '%F{008}%b%u%c%f' .     # 通常時のフォーマット $vcs_info_msg_0_ でプロンプトに表示する
zstyle ':vcs_info:git:*' actionformats '%F{008}%b%u%c%f | %a'  # コンフリクトが発生した時などはこちらが表示される %a は発生したアクション名に対応

PROMPT='
%F{032}%~%f $vcs_info_msg_0_
%F{magenta}%B❯%b%f '

スクリーンショット 2021-12-08 1.50.49.png

プロンプトは untracked files の状態表示などに対応しておらず、関数を作成していく必要がありそうです。
https://zenn.dev/kaityo256/articles/zsh-vcs-prompt

このあたりは Prezto を使うと手間をかけずにいい感じに表示できて楽なんですが、手間をかけて少しずつ便利にしていくのも楽しいものですね。
また弄っていきたいと思います。

4
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
4
1