【fish】ghqとpeco(fzf)を組み合わせたらリポジトリ管理がめっちゃ快適になった

fish

【fish】ghqとpeco(fzf)を組み合わせたらリポジトリ管理がめっちゃ快適になった

みなさんghqはお使いでしょうか?

ghqはGo言語で作成されたツールで、リポジトリを一元管理してくれるツールです。

GitHub - x-motemen/ghq: Remote repository management made easy
Remote repository management made easy. Contribute to x-motemen/ghq development by creating an account on GitHub.

例えば下記のように”ghq get”を実行すると、”~/.ghq/github.com”配下にリポジトリをcloneします。

$ ghq get https://github.com/motemen/ghq
# git clone https://github.com/motemen/ghq ~/.ghq/github.com/motemen/ghq

cloneしたリポジトリは”~/.ghq/github.com”で管理されているので、pecoまたはfzfでリポジトリ一覧をフィルタリングし、選択したディレクトリにcdするようにすれば、リポジトリへの移動が非常に楽になります。

fish shell、fzfの設定については下記の記事を参照ください。

【Mac】fish + fisher + peco + fzfで快適なターミナル環境を構築
fishはbashやzshと同じくシェルの1種です。以前から興味はあったのですが、Macを買い替えたのでzshから乗り換えられるか試してみることにしました。今回はfishとfisher、pecoを使ってターミナル環境を構築します。...
スポンサーリンク

ghqのインストール

ghqを使うにはGoをインストールする必要があるので、brewでインストールします。

$ brew install go

Goをインストールしたら、”~/.config/fish/config.fish”で”GOPATH”を設定します。

# Golang
set -x GOPATH $HOME/dev
set -x PATH $PATH $GOPATH/bin

“go get”を実行してghqをインストールします。

$ go get github.com/motemen/ghq
$ source ~/.config/fish/config.fish

以降はpecoまたはfzfで使いたい方を設定してください。

どちらも”Ctrl+G”でリポジトリ一覧を表示できるようになります。

ghq + pecoの設定

pecoを使う場合の設定は下記の通りです。
“~/.config/fish/config.fish”に設定してください。

# ghq + peco
function ghq_peco_repo
  set selected_repository (ghq list -p | peco --query "$LBUFFER")
  if [ -n "$selected_repository" ]
    cd $selected_repository
    echo " $selected_repository "
    commandline -f repaint
  end
end

# fish key bindings
function fish_user_key_bindings
  bind /cg ghq_peco_repo
end

ghq + fzfの設定

fisherでプラグインを追加します。

$ fisher add decors/fish-ghq

プラグインを追加しただけでも CTRL+G で動くのですが、fzfを”–reverse –height=100%”で表示したいので、functionを追加します。
※自分がわかっていないだけで、オプション等で設定できるのかもしれません。

“~/.config/fish/config.fish”に設定してください。

# ghq + fzf
function ghq_fzf_repo -d 'Repository search'
  ghq list --full-path | fzf --reverse --height=100% | read select
  [ -n "$select" ]; and cd "$select"
  echo " $select "
  commandline -f repaint
end

# fish key bindings
function fish_user_key_bindings
  bind \cg ghq_fzf_repo
end