【Mac】fish + fisher + peco + fzfで快適なターミナル環境を構築

fish

fishはbashやzshと同じくシェルの1種です。

以前から興味はあったのですが、Macを買い替えたのでzshから乗り換えられるか試してみることにしました。

今回はfishとfisher、pecoを使ってターミナル環境を構築します。

スポンサーリンク

フォントを設定

powerlineフォントをインストール

fishを使用する際、テーマによっては文字化けすることがあるため、powerlineフォントをインストールしてターミナルやiTerm2のフォントを変更しておきます。

$ git clone https://github.com/powerline/fonts.git --depth=1
$ cd fonts
$ ./install.sh
$ cd ..
$ rm -rf fonts/

iTerm2のフォントを変更

「Profiles > Text > Font」を選択し、フォントを変更します。

fishのインストール

fishをインストールします。

$ brew install fish

もしfishをインストールしても”/etc/shells”に”/usr/local/bin/fish”が追加されていない場合は、次のコマンドで追加してください。

$ echo /usr/local/bin/fish | sudo tee -a /etc/shells

シェルをfishに変更する

“chsh”を実行してシェルをfishに変更します。すぐに反映されない場合は、ターミナルで新しいセッションを開いてください。

$ chsh -s /usr/local/bin/fish
GitHub - fish-shell/fish-shell: The user-friendly command line shell.
The user-friendly command line shell. Contribute to fish-shell/fish-shell development by creating an account on GitHub.

fisher

fisherはfishのパッケージ管理ツールです。

GitHub - jorgebucaran/fisher: A plugin manager for Fish.
A plugin manager for Fish. Contribute to jorgebucaran/fisher development by creating an account on GitHub.

fisherのインストール

$ curl https://git.io/fisher --create-dirs -sLo ~/.config/fish/functions/fisher.fish     

fisherでプラグインを追加

fisherでプラグインを追加するには、次のように”fisher add”を使用します。

$ fisher add oh-my-fish/theme-agnoster

jethrokuan/z

GitHub - jethrokuan/z: Pure-fish z directory jumping
Pure-fish z directory jumping. Contribute to jethrokuan/z development by creating an account on GitHub.

“jethrokuan/z”は訪れたことのあるディレクトリを記録しておき、そのディレクトリに移動できるようにするツールです。

“z”はスペースの後にタブで補完することができるので、ディレクトリの移動が楽になります。

“z -l”でトラックしているディレクトリのリストを表示することもできます。

“z”はfisherを使ってインストールします。

$ fisher add jethrokuan/z

0rax/fish-bd

GitHub - 0rax/fish-bd: Quickly go back to a parent directory up in your current working directory tree. Don't write 'cd ../../..' redundantly, use bd instead.
Quickly go back to a parent directory up in your current working directory tree. Don't write 'cd ../../..' redundantly, use bd instead. - GitHub - 0...

カレントディレクトリより上のディレクトリに戻るときに、”cd ../../..”みたいなことをしなくてよくなります。

$ fisher add 0rax/fish-bd

peco

pecoは使い勝手の良いフィルタリングツールです。

動作イメージはGitHubにあるので、そちらを参照ください。

GitHub - peco/peco: Simplistic interactive filtering tool
Simplistic interactive filtering tool. Contribute to peco/peco development by creating an account on GitHub.

コマンドの実行履歴の絞り込みには”peco”か、もしくは後述の”fzf”のどちらか一方で良いと思います。

筆者は比較のために両方インストールしましたが、お好みの方を選んでください。

pecoのインストール

peco本体とプラグインを追加します。

$ brew install peco
$ fisher add oh-my-fish/plugin-peco

Ctrl+Rで履歴をフィルタリングできるようにする

“~/.config/fish/config.fish”に次の設定内容を追加します。

function peco_select_history_order
  if test (count $argv) = 0
    set peco_flags --layout=top-down
  else
    set peco_flags --layout=bottom-up --query "$argv"
  end

  history|peco $peco_flags|read foo

  if [ $foo ]
    commandline $foo
  else
    commandline ''
  end
end

function fish_user_key_bindings
  bind /cr 'peco_select_history_order' # Ctrl + R
end

あとは設定を再読込するかターミナルを再起動させると、「Ctrl+R」でpecoを使ってヒストリーをフィルタリングできるようになります。

fzf

fzfはあいまい検索で絞り込みができるコマンドラインツールです。

GitHub - junegunn/fzf: A command-line fuzzy finder
:cherry_blossom: A command-line fuzzy finder. Contribute to junegunn/fzf development by creating an account on GitHub.

fzfのインストール

fzfとプラグインを追加します。

$ brew install fzf
$ fisher add jethrokuan/fzf

“~/.config/fish/config.fish”に下記の設定を追加します。

set -U FZF_LEGACY_KEYBINDINGS 0
set -U FZF_REVERSE_ISEARCH_OPTS "--reverse --height=100%"

“FZF_LEGACY_KEYBINDINGS”はfishの過去バージョンとコンフリクトが発生していたため、新しいキーバインドを使用するのに必要だからです。

“FZF_REVERSE_ISEARCH_OPTS”は履歴のフィルタリング時に入力欄をターミナル上部に表示するための”–reverse”オプションと、フルスクリーンで表示するための”–height”オプションを設定しています。

GitHub - jethrokuan/fzf: Ef-🐟-ient fish keybindings for fzf
Ef-🐟-ient fish keybindings for fzf. Contribute to jethrokuan/fzf development by creating an account on GitHub.

ターミナルを開き直して「Ctrl+R」で履歴のフィルタリングに”fzf”を使えるようになります。

もし前述の”peco”を設定している場合は、キーバインドが重複しないように注意ください。