LoginSignup
3
2

More than 5 years have passed since last update.

fishでも"git "って打つのめんどくさい…!

Last updated at Posted at 2017-12-06

前回,fishのコマンドをgitのサブコマンドとして呼び出せるラッパーを書きました.

で,実はこのラッパー,git-という名前の関数を作ると「無引数でgitを実行した場合の挙動」を上書きできます.

この性質,うまく利用できないかと思っていたのですが

"git " って打つのめんどくさい…!

という記事を参考に,次の関数を書いてみました.

git-.fish
function git-
    if functions -q __git_mode
        functions -e __git_mode
    else
        function __git_mode -e fish_postexec
            commandline "git "
        end
    end
end

これを定義しておくと,gitと一度実行すると,次回以降コマンドラインにgitが挿入された状態になります.もう一度gitを空引数で実行するとgitの自動挿入が解除されます.便利!
git-.mov.gif

余談

ちなみに,元ネタの"git " って打つのめんどくさい…!で紹介されているsubshコマンドをfishで書くと以下のようになります.

subsh.fish
function subsh
# 指定した文字列を毎回コマンドライン先頭に挿入するようにする関数

    set -l sub "$argv"

    # --inherit-variable でフック定義時の値をキャプチャ
    function __subsh_hook -e fish_postexec --inherit-variable sub
        if test -n "$sub" -a -n "$argv"
            commandline "$sub "
        else
            # 空エンターあるいは引数なしの`subsh`で終了
            functions -e __subsh_hook 
        end
    end
end

fish版の面白いところは,フック関数__subsh_hookが呼ばれるタイミングでは変数subが存在しないのに,関数内部でsubを使えるところです.つまり,オプション--inherit-variableに渡された変数は関数の定義時に値が確定します.

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