LoginSignup
10
16

More than 3 years have passed since last update.

Macのzshでプロンプトをカスタマイズしたり、コマンド履歴を検索したりできるようにする

Last updated at Posted at 2017-09-30

Mac OS での、zsh のカスタマイズメモです。

とりあえず Preztoanyframe を組み合わせて、

  • プロンプトに Gitの状態などを表示。
  • peco でコマンドの実行履歴などを検索。

できるようにしてます。

Preztoのセットアップ

zsh を機能拡張するためのフレームワーク。
oh-my-zshより軽いらしい。

インストール

$ cd ~
$ zsh

$ git clone --recursive https://github.com/sorin-ionescu/prezto.git "${ZDOTDIR:-$HOME}/.zprezto"

# すでに ~/.zshrc がある場合はエラーになるので、削除するなり退避するなりしておく。
$ setopt EXTENDED_GLOB
for rcfile in "${ZDOTDIR:-$HOME}"/.zprezto/runcoms/^README.md(.N); do
  ln -s "$rcfile" "${ZDOTDIR:-$HOME}/.${rcfile:t}"
done

$ chsh -s /bin/zsh

詳細は PreztoのREADME 参照。

設定

  • 基本の設定ファイルは ~/.zshrc
  • # Customize to your needs... 以下に独自の設定を書いていく。

プロンプトの変更

プロンプトの見た目(テーマ)を変更。

  • プロンプトの設定は ~/.zpreztorc で。
~/.zpreztorc
  :

#
# Prompt
#

# Set the prompt theme to load.
# Setting it to 'random' loads a random theme.
# Auto set to 'off' on dumb terminals.
zstyle ':prezto:module:prompt' theme 'sorin' ←ここでテーマ名を指定

  :
  • $ prompt -p で標準的なテーマの見た目をプレビューできるので、気に入ったテーマ名を指定。

  • 独自にテーマを作りたい場合は、~/.zprezto/modules/prompt/functions ディレクトリに、prompt_任意のテーマ名_setup でテーマの定義ファイルを作ればOK。

おまけ

PreztoのREADME に、zshのチートシートがある。

anyframeのセットアップ

peco を使ってコマンドの実行履歴などを検索するのを簡単に設定できる zshプラグイン。

詳細は作者の方のQiita記事 zshでpecoと連携するためのanyframeというプラグインを作った を参照。

インストール

  • pecopercolfzfをインストール。
$ brew install peco
$ brew install percol
$ brew install fzf
  • anyframeのインストール
$ cd ~

$ git clone https://github.com/mollifier/anyframe

~/.zshrc に以下を追記。

~/.zshrc
  :

# Customize to your needs...

# anyframe
fpath=(~/anyframe(N-/) $fpath)

autoload -Uz anyframe-init
anyframe-init

設定

~/.zshrc に、anyframeウィジェット を呼び出すキーバインドをお好きなように定義。

Emacs使いならこんな感じ。

~/.zshrc
  :

# Customize to your needs...

# anyframe
  :

# Ctrl+x -> b
# peco でディレクトリの移動履歴を表示
bindkey '^xb' anyframe-widget-cdr
autoload -Uz chpwd_recent_dirs cdr add-zsh-hook
add-zsh-hook chpwd chpwd_recent_dirs

# Ctrl+x -> r
# peco でコマンドの実行履歴を表示
bindkey '^xr' anyframe-widget-execute-history

# Ctrl+x -> Ctrl+b
# peco でGitブランチを表示して切替え
bindkey '^x^b' anyframe-widget-checkout-git-branch

# Ctrl+x -> g
# GHQでクローンしたGitリポジトリを表示
# See https://github.com/x-motemen/ghq
bindkey '^xg' anyframe-widget-cd-ghq-repository

コマンド実行履歴のウィジェットをカスタマイズ

コマンドの実行履歴を表示するウィジェット anyframe-widget-execute-history は、デフォルトでは履歴から選択したコマンドが そのまま実行 される。

これだと、過去に実行したコマンドのパラメタをちょっと変更して実行したいって場合に不便なので、
選択したコマンドを 実行するのではなくプロンプトに表示するだけ にする。

/anyframe/anyframe-functions/widgets/anyframe-widget-execute-history
anyframe-source-history \
  | anyframe-selector-auto "$LBUFFER" \
  | anyframe-action-insert  # ← anyframe-action-execute から anyframe-action-insert に変更

以上

10
16
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
10
16