dshimizu/blog/alpha

とりとめのないITブログ

fish shell をインタラクティブシェルとして使う

はじめに

数年前に fish というシェルがあるのを知ってそのままでしたが、また最近耳にしたので少し触ってみながら、インタラクティブシェルとして使うよう設定してみました。

fish とは

fish は friendly interactive shell の略らしく、シェルの一つです。

fishshell.com

github.com

試す

macOS の環境にインストールしてみます。

環境

% sw_vers
ProductName:        macOS
ProductVersion:     13.6
BuildVersion:       22G120

インストール

ドキュメントにある通り、Homebrew でインストールできます。

% brew install fish

設定ファイルを見る

設定ファイルは下記にあるようです。.bottle は Homebrew のものなので無視して他のファイルを見てみます。

% find / -type f -name config.fish -not -path '/System/Volumes/Data/*'  2> /dev/null
/usr/local/etc/fish/config.fish
/usr/local/Cellar/fish/3.6.1/.bottle/etc/fish/config.fish
/usr/local/Cellar/fish/3.6.1/share/fish/config.fish
/usr/local/share/fish/config.fish
/Users/dshimizu/.config/fish/config.fish

/usr/local/etc/fish/config.fishの中身は初期時点では全てコメントアウト状態のようでした。 グローバルな設定をこのファイルか、 /usr/local/etc/fish/conf.d 配下に配置するようです。

% cat /usr/local/etc/fish/config.fish
# Put system-wide fish configuration entries here
# or in .fish files in conf.d/
# Files in conf.d can be overridden by the user
# by files with the same name in $XDG_CONFIG_HOME/fish/conf.d

# This file is run by all fish instances.
# To include configuration only for login shells, use
# if status is-login
#    ...
# end
# To include configuration only for interactive shells, use
# if status is-interactive
#   ...
# end

/usr/local/share/fish/config.fish/usr/local/Cellar/fish/3.6.1/share/fish/config.fishシンボリックリンクでした。

% ls -l /usr/local/share/fish/config.fish
lrwxr-xr-x  1 dshimizu  admin  46 10 17 01:02 /usr/local/share/fish/config.fish -> ../../Cellar/fish/3.6.1/share/fish/config.fish

/usr/local/Cellar/fish/3.6.1/share/fish/config.fish を見ると、色々処理が書かれてました。ただ、このファイルの冒頭に、このファイルの削除や編集は推奨しない、とありました。

% cat /usr/local/Cellar/fish/3.6.1/share/fish/config.fish
# This file does some internal fish setup.
# It is not recommended to remove or edit it.
#
# Set default field separators
#
 :
 :

~/.config/fish/config.fish は、以下のような状態でした。

~/.config/fish/ 以下に、ユーザー個別の設定を配置していくようです。

% cat ~/.config/fish/config.fish
if status is-interactive
    # Commands to run in interactive sessions can go here
end

使ってみる

fish を起動してみます。

% fish
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
dshimizu@MacBook-Pro ~>

上記では表現できてませんが、デフォルトで色がついています。 また、コマンド入力をしていると、補完やハイライトがかなり良い感じで出てきます。

GUI 設定

fish_config コマンドを実行すると、GUI の画面が起動します。

dshimizu@MacBook-Pro ~> fish_config

この時、ブラウザが起動して http://localhost:8000/********** といったURLでリクエストが飛びますが、ブラウザが https で処理しようとすると ERR_SSL_PROTOCOL_ERROR というエラーになるので、 その場合は http でアクセスし直すと画面が表示されます。

fish のインタラクティブシェル設定

fish は POSIX 非互換のシェルであるようで、デフォルトのシェルとして扱うのは少々手間が多いかもしれないので、普段使いのインタラクティブシェルでのみ実行することにします。

ターミナル起動時に fish を実行するようにするには、多分 .zshrc に以下のような記述をすることでいけると思います。

if [[ -z ${BASH_EXECUTION_STRING} || -z ${ZSH_EXECUTION_STRING} && -t 1 ]]
then
    exec fish
fi

まとめ

fish シェルの初期設定ファイルを眺めつつ、インタラクティブシェルとして使うようにしてみました。 まだ自分に合った設定は見えておらず、また、プラグイン等あるようなのでちょっとずつ使っていければと思います。

参考