コミットメッセージ用の「シェル内」エディタを作成できますか?

コミットメッセージ用の「シェル内」エディタを作成できますか?

git commitこのコマンドのbashエイリアスを作成したいと思います。私は通常次のように使用するので、次のgit commit -m "my message here"エイリアスを作成しました。

alias commit='git commit'

これにより、次のことができます。

$ commit -m "My message"

しかし、エイリアスを更新してその-mオプションを削除することもできると思いました。

$ commit "my message"

それから「まだ引用符を入力する必要がありますか?」と思いました。私が想像した内容は次のとおりです。

$ commit
> My message here

>引用符付き文字列にEnterを入力すると、表示される引用符は引き続き次の場所を示します。

$ git commit -m "I am about to hit the enter key
> 

Enter キーを押すとコマンドが完了します。

Bashでこの動作をスクリプト化する方法はありますか?

Ctrlボーナス:+をクリックするとコマンドが中断されますc

ベストアンサー1

bash'を使用する関数を書くことができますread -e

commit() {
  local commitlog
  IFS= read -rep '> ' commitlog &&
     git commit -m "$commitlog" "$@"
}

read -eを押して改行を挿入できますが、1行だけ読み取られるため、Ctrl+VCtrl+J最初の改行以降のすべての内容は削除されます。read

-d $'\r'オプションを追加することでこの問題を解決できますがreadEnter少なくともバージョン 5.0.7 および 4.4.19 では、プロンプト後の後続の処理がめちゃくちゃであることがわかりました。

とにかく簡単ですzsh

commit() {
  local commitlog=
  vared -ep '> ' commitlog &&
    git commit -m "$commitlog" "$@"
}

改行文字を入力するには、Ctrl+VCtrl+Jlike in または with を使用できます。bashAlt+Enter

または記録(重複を避けるためにここで共有):

commit() {
  emulate -L zsh
  setopt sharehistory histignorealldups histsavenodups
  local commitlog=
  fc -ap ~/.zcommit-history 500
  vared -ehp '> ' commitlog || return
  print -rs -- "$commitlog"
  git commit -m "$commitlog" "$@"
}

おすすめ記事