11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ghqとpecoが便利すぎたので快適なcd(change directory)方法を紹介する【bash対応】

11
Last updated at Posted at 2018-11-10

みなさんは開発用ディレクトリに移動する場合どうしていますか?

ターミナルを立ち上げた後、cdコマンドでTABキーを何回も打鍵していませんか?

それ、もうやらなくていいんです。

そう、 ghqとpecoを組み合わせればね。

何ができるようになるの?

自分の好きなコマンドですぐに移動したいディレクトリ(ワークスペース)へ移動できます。

デモ

demo2.mov.gif

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) とします。

.bashrc
function peco-cd {
  cd "$( ghq list --full-path | peco)"
}

alias sd='peco-cd'
source ~/.bashrc
または
source ~/.bash_profile

おめでとうございます!
これで快適なcd方法を手に入れました!

それではまたどこかでお会いしましょう!
Happy Coding!

参考文献

11
10
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
11
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?