This is my obligatory, "it's been a while" post. My apologies. Much has happened since my last post, I've moved into a new house, a pandemic, and I now have a new day job. At any rate, all that to say I'm hoping to get back on track with my posts. As a special treat I've created my first video content and I hope to do more of this as well. Today's topic is on tmux and how I use it to manage my multiple projects concurrently. I have a couple of scripts that I use to manage all of this.
My workflow goes something like this:
-
Script to easily create a project session I've created a script called
dev
that will take the name of the current directory, create a tmux session by that name, then create the windows that I like to use, run a specific command in those windows, then set the focus on the first window. If I detach from the session, then later run the dev script a second time from the same directory, instead of creating a new session it will attach to the one that was already created.#! /usr/bin/env bash directory_basename=${PWD##*/} if ! tmux has-session -t "$directory_basename" then tmux new-session -s "$directory_basename" -n Server -d # create moar windows tmux new-window -t "$directory_basename":1 -n UI tmux new-window -t "$directory_basename":2 -n Tests tmux new-window -t "$directory_basename":3 -n Code tmux new-window -t "$directory_basename":4 -n Zsh # run some command in the first window if [ -z "$1" ] then tmux send-keys -t "$directory_basename":0.0 "cd ${PWD}" C-m else tmux send-keys -t "$directory_basename":0.0 "cd ${PWD} && ${1}" C-m fi tmux send-keys -t "$directory_basename":1.0 "cd ${PWD}" C-m tmux send-keys -t "$directory_basename":2.0 "cd ${PWD}" C-m tmux send-keys -t "$directory_basename":3.0 "cd ${PWD}" C-m tmux send-keys -t "$directory_basename":4.0 "cd ${PWD}" C-m # select the server window and pane tmux select-window -t "$directory_basename":0.0 fi tmux attach -t "$directory_basename"
-
Script to easily attach to a project session
If I detach from a session, then do some work unrelated to any of the sessions I've created and find myself in some distant unrelated directory, my
dev
script won't help me get back to any of my tmux sessions. Sure I could list them out and attach to them manually but that is a lot of typing, and developers are notoriously lazy, myself included. So I've created a script I call tm that will list each tmux session and assign a numeric index to each, then you can choose one of the numbers and it will attach to that session, it's that simple. If you don't see the session you want to attach to, any character other than one of the indexes will exit the script. I generally choose q. You would put this script in your .zshrc or .bashrc, then just call it like any other command.# present a list of tmux sessions to choose from function tm() { select sel in $(tmux ls -F '#S'); do break; done if [ -z "$sel" ] then echo "You didn't select an appropriate choice" else tmux attach -t "$sel" fi }
Categories: #technology