この投稿で使用しているOS、fishのバージョン
- macOS Big Sur 11.1 / macOS Catalina 10.15 / Ubuntu 20.04
- fish shell v3.2.0
困ったこと
fish 3.1.xで以下のように、空エンターを入力するとカレントディレクトリがgitリポジトリである場合は git status を実行するような設定をしていて快適に動作していました。
.config.fish
# コマンド実行前のイベントにフックする
function done_enter --on-event fish_postexec
# コマンドに入力された文字数が 0文字=空エンター なら
if test -z "$argv"
# カレントディレクトリがgitリポジトリなら git status を実行する
if git rev-parse --is-inside-work-tree > /dev/null 2>&1
echo
echo (set_color yellow)"--- git status ---"(set_color normal)
git status -sb
end
end
end
ですが、fish 3.2.0 から fish_preexec/fish_postexec が 空エンターでは実行されなくなってしまいました。
実際にヘルプでも説明しています。
$ function -h
· fish_preexec, which is emitted right before executing an interactive command. The commandline is passed as the first parameter. Not emitted if command is empty.
· fish_postexec, which is emitted right after executing an interactive command. The commandline is passed as the first parameter. Not emitted if command is empty.
Not emitted if command is empty.
v3.2.0でも、空エンター時に処理を実行する方法がないか調べました。
対処
Ctrl+mに処理をキーバインドするとエンター押下時にも実行されるため、これでどうにかします。
(なぜ Ctrl+m がエンターとして扱われるのかよくわかっていない・・・)
.config.fish
function fish_user_key_bindings
bind \cm done_enter # Ctrl+m にキーバインドするとエンターにも反応する
end
function done_enter
# 現在の入力文字数が0文字なら
if test -z (commandline)
echo
# カレントディレクトリがgitリポジトリなら git status を実行する
if git rev-parse --is-inside-work-tree > /dev/null 2>&1
echo
echo (set_color yellow)"--- git status ---"(set_color normal)
git status -sb
end
else
# 何か入力されているなら現在の入力を実行する
# eval (commandline) だとヒストリーに残らない
commandline -f execute
end
# 入力をクリアする
commandline -f repaint
end
とりあえずこれで今まで通り動作しています!