28
A Workflow for Cloning and Launching Git Repositories in New tmux Windows
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:
- In the Command Palette, select:
> Git: clone
- Enter the repo to clone
- 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.
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 .
}
What happens when you run $ git-clone-tmux
?
-
[ -z "${repo}" ] && read -e -p "Repo: " repo
- If
${repo}
is empty, then prompt user interactively
- If
-
[ -z "${directory}" ] && read -e -p "Directory: " -i "${HOME}/Work" directory
- If
${directory}
is empty, then prompt user interactively
- If
-
if [[ "${repo}" =~ ^github ]]
- If
${repo}
starts withgithub
, then usegh
to clone the repo
- If
-
if [[ "${repo}" =~ ^gitlab ]]
- If
${repo}
starts withgitlab
, then useglab
to clone the repo
- If
- otherwise, assume
${repo}
starts withhttps://
-
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 .
- Launch new tmux window, setting the working directory to the newly cloned repo, and run
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'
Or add the following to your git config file:
[alias]
clone-tmux = !/usr/bin/env bash -ic git-clone-tmux
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!
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!
28