LoginSignup
0
0

More than 5 years have passed since last update.

fishでtypoしたときに大文字をスペースと小文字に展開して実行する

Last updated at Posted at 2017-10-30

SanSを使っていると、たとえばgit stと入力したい時に、よくgitStと入力してしまう。そこで、zshでtypoしたときに大文字をスペースと小文字に展開して実行するのスニペットを用いていたのだが、シェルをFishに変えてから、またしょっちゅう入力間違ってストレスが再発した。

というわけで、上記のスクリプトをFish用に移植した。

$HOME/.config/fish/functions/try_fixing_camelized_command_input.fish
function try_fixing_camelized_command_input --on-event fish_command_not_found
    set new (echo $argv | ruby -pe 'sub(/[A-Z]/){" "+$&.downcase}')

    if [ "$argv" != "$new" ]
        echo "=> $new"
        eval $new
        return 0
    end

    return 127
end

ドキュメントにある通り、コマンドが見つからなかった場合には、Fishがfish_command_not_foundというイベントをemitしてくるので、これをつかまえてハンドリングしたらよい。

これで、gitStと入力すると、なんかいい感じにgit stに変えて実行してくれるようになった。

$ gitSt
fish: Unknown command 'gitSt'
=> git st
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working tree clean

わあい。

追記

ドキュメントによると、イベントハンドラーは自動で読み込まれないようなので、一度config.fishの中とかで実行してあげないとならないぽい。

sh:$HOME/.config/fish/config.fish
# try_fixing_camelized_command_input
try_fixing_camelized_command_input
0
0
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
0
0