🐙

ghqとpecoでリポジトリ管理を楽にする(wsl2またはMac)

2022/06/13に公開

筆者はwsl2で環境構築しています。

zshをインストールする

必要なければ飛ばしてください。

$ sudo apt install zsh
$ zsh --version
zsh 5.8 (x86_64-ubuntu-linux-gnu)

デフォルトをzshに変更する。

$ echo $SHELL
/bin/bash
$ chsh -s $(which zsh)

ターミナルを再起動したら、以下のように表示されます。

.zshrcなどのファイルがないのでどうするか聞かれています。

今回は空の.zshrcを作るので0を入力します。

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
~).  This function can help you with a few settings that should
make your use of the shell easier.

You can:

(q)  Quit and do nothing.  The function will be run again next time.

(0)  Exit, creating the file ~/.zshrc containing just a comment.
     That will prevent this function being run again.

(1)  Continue to the main menu.

(2)  Populate your ~/.zshrc with the configuration recommended
     by the system administrator and exit (you will need to edit
     the file by hand, if so desired).

--- Type one of the keys in parentheses ---

確認。

% echo $SHELL
/usr/bin/zsh

Homebrewをインストールする

すでにインストール済みであれば飛ばしてください。

$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

完了すると親切にNext Stepが表示されるので従います。

==> Next steps:
- Run these two commands in your terminal to add Homebrew to your PATH:
    echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/wsl_yosi/.zprofile
    eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
- Install Homebrew's dependencies if you have sudo access:
    sudo apt-get install build-essential
  For more information, see:
    https://docs.brew.sh/Homebrew-on-Linux
- We recommend that you install GCC:
    brew install gcc
- Run brew help to get started
- Further documentation:
    https://docs.brew.sh
$ echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.zshrc
$ eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
$ source ~/.zshrc
$ brew --version
Homebrew 3.5.1
Homebrew/homebrew-core (git revision 0712e880ba1; last commit 2022-06-12)

ついでにコンパイラもインストールしておきます。

$ sudo apt-get install build-essential

ghq と peco のインストール

$ brew install go

$ brew install ghq
$ git config --global ghq.root '~/projects'

$ brew install peco

vim ~/.zshrcもしくはテキストエディタで.zshrcを開いて以下を追加

function peco-projects () {
  local selected_dir=$(ghq list -p | peco --query "$LBUFFER")
  if [ -n "$selected_dir" ]; then
    BUFFER="cd ${selected_dir}"
    zle accept-line
  fi
  zle clear-screen
}
zle -N peco-projects
bindkey '^]' peco-projects
$ source ~/.zshrc

ghq と peco の使い方

ghqでclone

ghq get git@github.com:yosipy/one_time_password.git

リポジトリ一覧表示

Ctrl + ]を押すと表示される。
文字入力で検索してからのエンター、もしくは十字キーとエンターで選択できる。

参考

https://zenn.dev/obregonia1/articles/e82868e8f66793

https://scrapbox.io/fuga-zakki/WSL2環境の構築

Discussion