0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pecoのkeymapを最低限で修正してgit addを幸せにする

Last updated at Posted at 2025-08-15

概要

  • git add の選択がめんどくさかった
  • peco 導入してたのでgit addを簡易にしてくれるやつを参考にしてzshを用意した
  • key bindをvimっぽくしたかったので、pecoに簡易の設定をして、幸せ(?)にした

terminalでのgit add

  1. git status
  2. ステージに追加したいファイルをコピペして git addする(これがちょっとめんどくさい)
  3. 繰り返す

pecoで楽にできないか

ぐぐったらすぐ見つかった
https://masawada.hatenablog.jp/entry/2015/06/30/165156

これをzshに書き換える(Geimini)

# git add + peco
# peco を使ってインタラクティブに `git add` する関数
gap() {
  # gitリポジトリのルートディレクトリを取得
  local search_root
  search_root=$(git rev-parse --show-toplevel 2>/dev/null)

  # gitリポジトリでなければエラーメッセージを表示して終了
  if [ -z "$search_root" ]; then
    echo "ここはgitリポジトリではありません。" >&2
    return 1
  fi

  # git statusの結果からpecoでファイルを選択
  # awkでファイルパスだけを抽出
  local selected_files
  selected_files=$(git status --porcelain | peco | awk '{print $2}')

  # ファイルが選択されなかった場合は終了
  if [ -z "$selected_files" ]; then
    echo "ファイルが選択されませんでした。"
    return
  fi

  # 選択されたファイルを一つずつ `git add` する
  # ルートディレクトリに移動してから実行することで、どのサブディレクトリからでも正しく動作する
  (cd "$search_root" && echo "$selected_files" | tr ' ' '\n' | while read -r file; do
    git add "$file"
    echo "Added: $file"
  done)
}

~/.zshrc に追記して、source ~/.zshrc で使えるようになる

vimライクにする

試したらたしかに便利だけど、物足りなさがある

  • 矢印キーで操作
  • 複数選択はCtrl + Space(すでにキーボードショートカットキー設定されている)

ので、こうしたかった

  • vimのk(up)、j(down)にする
  • spaceで複数選択できるようにする

調べてみたら、~/.peco/config.jsonで、keymapを変更できるよう。試す

{
  "Keymap": {
    "k": "peco.SelectUp",
    "j": "peco.SelectDown",
    "Space": "peco.ToggleSelectionAndSelectNext"
  }
}

幸せ(?)

output.gif

0
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?