みなさんは開発用ディレクトリに移動する場合どうしていますか?
ターミナルを立ち上げた後、cdコマンドでTABキーを何回も打鍵していませんか?
それ、もうやらなくていいんです。
そう、 ghqとpecoを組み合わせればね。
何ができるようになるの?
自分の好きなコマンドですぐに移動したいディレクトリ(ワークスペース)へ移動できます。
デモ
ghqとは
ghqのREADME.mdから和訳
Remote repository management made easy
リモートリポジトリの管理を楽にする
'ghq' provides a way to organize remote repository clones, like go get does. When you clone a remote repository by ghq get, ghq makes a directory under a specific root directory (by default ~/.ghq) using the remote repository URL’s host and path.
'ghq'はgo getのようにリモートリポジトリをクローンする方法を提供します。 ghq getでリモートリポジトリをクローンすると、ghqはリモートリポジトリURLのホストとパスを使って特定のルートディレクトリ(デフォルトでは~/.ghq)の下にディレクトリを作成します。
例えば、 git@github.com:motemen/ghq.git をcloneする場合
ghq get git@github.com:motemen/ghq.git
ghqが参照するルートディレクトリ/github.com/motemen/ghq 配下にリポジトリをクローンします。
ghqが提供しているコマンド list を利用すると ghqが参照するルートディレクトリ配下のリポジトリを出力するので、この機能を利用します。
pecoとは
https://github.com/peco/peco
README.mdにわかりやすいデモが記載されているため割愛
環境構築手順
Goをインストール
brew install go
GOPATHを設定(パスはお好みの値を設定してください)
export GOPATH=$HOME/go
ghqをインストール
go get github.com/motemen/ghq
ghqが参照するルートディレクトリの設定
git config --global ghq.root $GOPATH/src
pecoをインストール
go get github.com/peco/peco/cmd/peco
.bashrcか.bash_profileにコマンドのエイリアスを書く
エイリアス名はお好みで。
今回は sd (select directory) とします。
function peco-cd {
cd "$( ghq list --full-path | peco)"
}
alias sd='peco-cd'
source ~/.bashrc
または
source ~/.bash_profile
おめでとうございます!
これで快適なcd方法を手に入れました!
それではまたどこかでお会いしましょう!
Happy Coding!
参考文献
