tmux + zsh + fzf + ghqでセッションを作成して,指定リポジトリに移動する処理

挙動

  • tmuxを開いている状態で,C-gを押すとghqでgetしてきたリポジトリのリストを取得する.
  • 移動したいリポジトリを選択する.
  • 新規セッションが作成され,そのセッションに移動する.

コード

これを.zshrcに書いたら動くと思います.

function create_session_with_ghq() {
    # fzfで選んだghqのリポジトリのpathを取得
    moveto=$(ghq root)/$(ghq list | fzf)

    if [[ ! -z ${TMUX} ]]
    then
        # リポジトリ名を取得
        repo_name=`basename $moveto`

        # repositoryが選択されなかった時は実行しない
        if [ $repo_name != `basename $(ghq root)` ]
        then
            # セッション作成(エラーは/dev/nullへ)
            tmux new-session -d -c $moveto -s $repo_name  2> /dev/null

            # セッション切り替え(エラーは/dev/nullへ)
            tmux switch-client -t $repo_name 2> /dev/null
        fi
    fi
}
zle -N create_session_with_ghq
bindkey '^G' create_session_with_ghq

エラーもみ消している感

こんな感じで動く

f:id:MitubaEX:20180502234529g:plain

参考にしました

ghq+fzf on tmux | matsub

blog.chairoi.me