DEV Community

Marcin Wosinek
Marcin Wosinek

Posted on • Originally published at how-to.dev

How to create tmux session with a script

If you are a user of tmux, you probably find yourself re-creating the same session structure every time when you start a tmux. Although there are some projects meant to provide a way to write a configuration file & recreate a session base on it, such as:

both of them are Ruby-based, and if you are not a ruby developer it can feel like too much of a hassle to get a language interpreter & a package manager for such a task.

In this article, I will show you how to create a tmux window with tmux CLI commands, so you can enjoy the same window structure without having to create everything yourself manually.

Working example

The example script works like this:
asciicast

Code and explanation

First create the tmux-start.sh file & make it executable:

$ touch tmux-start.sh
$ chmod +x tmux-start.sh
Enter fullscreen mode Exit fullscreen mode

Then set the file content as follows:

#!/bin/bash
Enter fullscreen mode Exit fullscreen mode

Make the file executable with bash shell.

session="webpack-ts"

tmux new-session -d -s $session
Enter fullscreen mode Exit fullscreen mode

Creates a new session, and gives it a name. You cannot use spaces here and use the same name twice. Especially with nested sessions name collisions can end up weird - with windows nesting one another in an infinite loop.

window=0
tmux rename-window -t $session:$window 'git'
tmux send-keys -t $session:$window 'git fetch --prune --all' C-m
Enter fullscreen mode Exit fullscreen mode

window 0 is special because it's already created with a session - that's why we rename it, and not create it as all the other windows in the example. The second is sending some command to the window & execute it immediately - thanks to adding C-m after the command.

I call it git, because I like having all the preview git commands on the screen when I run a new one.

window=1
tmux new-window -t $session:$window -n 'vim'
tmux send-keys -t $session:$window 'vim package.json'
Enter fullscreen mode Exit fullscreen mode

Creates a new window called vim, and type the command to start the editor. I don't call it automatically - after getting to the window, I'll have to press enter myself.

window=2
tmux new-window -t $session:$window -n 'run'

window=3
tmux new-window -t $session:$window -n 'serve'
tmux send-keys -t $session:$window 'npm run serve'
Enter fullscreen mode Exit fullscreen mode

just creates two other windows

tmux attach-session -t $session
Enter fullscreen mode Exit fullscreen mode

Attaches the session to the current window.

The complete file ./tmux-start.sh:

#!/bin/bash

session="webpack-ts"

tmux new-session -d -s $session

window=0
tmux rename-window -t $session:$window 'git'
tmux send-keys -t $session:$window 'git fetch --prune --all' C-m

window=1
tmux new-window -t $session:$window -n 'vim'
tmux send-keys -t $session:$window 'vim package.json'

window=2
tmux new-window -t $session:$window -n 'run'

window=3
tmux new-window -t $session:$window -n 'serve'
tmux send-keys -t $session:$window 'npm run serve'

tmux attach-session -t $session
Enter fullscreen mode Exit fullscreen mode

Thanks

Many thanks to the author of Scripting A Tmux Work-space Start-up. The approach I'm presenting here I have it from there, plus I had to tweak it a bit to make sure it's always producing the same results even if I start multiple windows a the same time.

Summary

In this article, we have seen how to create a script that starts a tmux session for us. If you want, you can even chain few scripts like this to create nested windows.

Top comments (0)