2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

fish v3.2.0 で空エンター入力で fish_preexec / fish_postexec が反応しなくなってしまったのをどうにかする

Last updated at Posted at 2021-03-12

この投稿で使用している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

とりあえずこれで今まで通り動作しています!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?