DEV Community

Peter Benjamin (they/them)
Peter Benjamin (they/them)

Posted on

A Workflow for Cloning and Launching Git Repositories in New tmux Windows

Table of Contents

Overview

One thing I really like about Visual Studio Code is the ability to clone a repo and open it in the editor with little-to-no friction in 3 quick steps:

  1. In the Command Palette, select: > Git: clone
  2. Enter the repo to clone
  3. Select the directory to clone into

Then Visual Studio Code launches a new editor in the repo and I'm ready to go.

I want to have a similar experience in my terminal-based development workflow.

The Solution

At a high-level, my current solution is to invoke a bash function that prompts the user for the repo to clone and directory to clone it into, then run vim on the repo in a new tmux window.

Here is the implementation as a bash script:

git-clone-tmux() {
    local repo=""
    local directory=""
    local clone_path=""

    repo="${1}"
    directory="${2}"

    [ -z "${repo}" ] && read -e -p "Repo: " repo
    [ -z "${directory}" ] && read -e -p "Directory: " -i "${HOME}/Work" directory

    if [[ "${repo}" =~ ^github ]] || [[ "${repo}" =~ "${GH_HOST}" ]]; then
        clone_path="${directory}/${repo}"
        gh repo clone "${repo}" "${clone_path}"
    elif [[ "${repo}" =~ ^gitlab ]] || [[ "${repo}" =~ "${GL_HOST}" ]]; then
        clone_path="${directory}/${repo}"
        glab repo clone "${repo}" "${clone_path}"
    else
        clone_path="${directory}/$(basename ${repo})"
        git clone "${repo}" "${clone_path}"
    fi

    tmux new-window -c "${clone_path}" -n "${clone_path}" vim .
}
Enter fullscreen mode Exit fullscreen mode

What happens when you run $ git-clone-tmux?

  • [ -z "${repo}" ] && read -e -p "Repo: " repo
    • If ${repo} is empty, then prompt user interactively
  • [ -z "${directory}" ] && read -e -p "Directory: " -i "${HOME}/Work" directory
    • If ${directory} is empty, then prompt user interactively
  • if [[ "${repo}" =~ ^github ]]
    • If ${repo} starts with github, then use gh to clone the repo
  • if [[ "${repo}" =~ ^gitlab ]]
    • If ${repo} starts with gitlab, then use glab to clone the repo
  • otherwise, assume ${repo} starts with https://
  • tmux new-window -c "${clone_path}" -n "${clone_path}" vim .
    • Launch new tmux window, setting the working directory to the newly cloned repo, and run vim .

Bonus

You can create a git alias so that you can call this function as if it were a git subcommand.

Just run the following:

$ git config --global alias.clone-tmux '!/usr/bin/env bash -ic git-clone-tmux'
Enter fullscreen mode Exit fullscreen mode

Or add the following to your git config file:

[alias]
    clone-tmux = !/usr/bin/env bash -ic git-clone-tmux
Enter fullscreen mode Exit fullscreen mode

Now, you can run git clone-tmux <repo> <dir> or git clone-tmux for interactive prompting.

Extra bonus: shell tab completions work on git aliases too!

Summary

This is, by no means, the most elegant script, but it works for me needs.

I hope you enjoyed reading this article and I hope it inspires you to find ways to streamline and improve your workflow and productivity!

For more git alias goodies, check out my .config/git/config dotfile.

For more bash function goodies, check out my .functions dotfile.

Happy hacking!

Top comments (0)