How do I create a tmux session with multiple windows already opened? - bash

I've tried just about everything I can find online, but nothing is working. I have tried the following methods, and the usual result is a new tmux session with only one window.
Simply in .bashrc.
.bashrc
tmx () {
tmux new-session -A -s SessionName
tmux new-window -n Win1
tmux new-window -n Win2
tmux new-window -n Win3
tmux new-window -n Win4
tmux attach-session -d -t SessionName # with and without this line
tmux select-window -t Win1 # with and without this line
}
And again only in .bashrc.
.bashrc
tmx () {
tmux new-session -A -s SessionName ||
tmux \
neww -s Win1 \; \
neww -s Win2 \; \
neww -s Win3 \; \
neww -s Win4 \; \
selectw -t Win1
}
This following attempt would be my preferred method, as it makes the most sense to me.
Calling tmux without the first line makes all the other lines cause a "Session not found" error to occur. This makes no sense, since aren't we supposed to call tmux in order to reach this stuff anyway? My original plan was to make a session and have this file automatically set up my tmux.
.tmux.conf
new-session -A -s SessionName
new-window -t Win1
new-window -t Win2
new-window -t Win3
new-window -t Win4
attach-session -d -t SessionName # with and without this line
select-window -t Win1 # with and without this line
This method, whether using an alias or making a function, usually results in "failed to connect to server". But when fiddling with it enough for that not to happen, it produces the same result as the rest.
.bashrc
alias tmx='tmux source-file "~/.tmux/mysession"'
.tmux/mysession
new-session -A -s SessionName
new-window -t Win1
new-window -t Win2
new-window -t Win3
new-window -t Win4
attach-session -d -t SessionName # with and without this line
select-window -t Win1 # with and without this line
What am I doing wrong?

You need to create the session in detached mode (-d); otherwise, your script blocks until you detach from the new session. Likewise, your script will block after tmux attach-session until you detach, so you need to select the correct window first. Note that you can -d with new-window to avoid making each new window the current window, eliminating the need to call select-window at all.
Yes, -d gets used a lot.
tmx () {
# Use -d to allow the rest of the function to run
tmux new-session -d -s SessionName
tmux new-window -n Win1
# -d to prevent current window from changing
tmux new-window -d -n Win2
tmux new-window -d -n Win3
tmux new-window -d -n Win4
# -d to detach any other client (which there shouldn't be,
# since you just created the session).
tmux attach-session -d -t SessionName
}

Related

Is it impossible to write a functioning tmux shell script that launches 3 panes of ncmpcpp?

This is the second time I'm posting the question, because this entire situation ended up being more confusing than I thought.
I'm trying to make a script that opens tmux, splits it into 3 panes, makes them equally sized horizontally, and launches ncmpcpp in all of them. This is the script I managed to write:
#!/bin/sh
tmux new-session -d 'ncmpcpp'
printf 'creating session\n'
tmux split-window -h 'ncmpcpp'
printf 'window split 1\n'
tmux split-window -h 'ncmpcpp'
printf 'window split 2\n'
tmux select-layout even-horizontal
printf 'evening out panes\n'
tmux a #
printf 'attaching session\n'
I'm a beginner in writing scripts at all, so sorry if the mistake here is obvious. The printfs are there so that I could identify where exactly the script crashes tmux. Apparently, it crashes at tmux a #.
The weird thing is that, the crash happens instantly. I can't take a screenshot due to just how rapidly it disappears.
terminate called after throwing an instance of `std::logic_error
what() : constructed window couldn't fit into terminal'
Basically, the panes are too small for ncmpcpp to launch in. Even though I could manually launch ncmpcpp in way smaller terminals.
However, after some messing around, I accidentally "kind of" solved the problem.
I screwed around with the script, and I ended up with this:
#!/bin/sh
printf '\033[8;29;252t'
tmux new-session -d
tmux select-layout even-horizontal
tmux send-keys -t 0 'tput cols' Enter
tmux split-window -h
sleep 1
tmux select-layout even-horizontal
tmux send-keys -t 1 'tput cols' Enter
tmux split-window -h
sleep 1
tmux select-layout even-horizontal
tmux send-keys -t 2 'tput cols' Enter
sleep 1
tmux send-keys -t 0 'ncmpcpp' Enter
tmux send-keys -t 1 'ncmpcpp' Enter
tmux send-keys -t 2 'ncmpcpp' Enter
tmux a #
It takes a 3 second pause at the beginning, before immediately launching tmux, with 3 panes, all of them having ncmpcpp on them.
...Sometimes.
It doesn't work all the time, sometimes certain panes just crash outright, sometimes they don't.
I'm honestly really confused as to what's going on here. My guess is the size of the panes isn't recorded well, enough to screw up the value of how many columns/rows each pane has. But I'm honestly stumped.
That's why I'm asking this - ncmpcpp only launches when the size of the terminal is big enough. Since tmux messes with the actual values saying what the columns are, is it impossible to do the things I said in the title?
So
#!/bin/sh
printf '\033[8;29;252t'
tmux new-session -d -x 252 -y 29
tmux select-layout even-horizontal
tmux send-keys -t 0 'tput cols' Enter
tmux split-window -h
sleep 1
tmux select-layout even-horizontal
tmux send-keys -t 1 'tput cols' Enter
tmux split-window -h
sleep 1
tmux select-layout even-horizontal
tmux send-keys -t 2 'tput cols' Enter
sleep 1
tmux send-keys -t 0 'ncmpcpp -s playlist' Enter
tmux send-keys -t 1 'ncmpcpp -s media_library' Enter
tmux send-keys -t 2 'ncmpcpp -s clock' Enter
# help,playlist,browser,search_engine,media_library,
# playlist_editor,tag_editor,outputs,visualizer,clock
tmux a #
is your own answer. Have you tried altering the ncmpcpp layout:
(called with -c)
ncmpcpp -c ~/.my_ncmpcpp.conf
#~/.my_ncmpcpp.conf
#edited from https://bbs.archlinux.org/viewtopic.php?pid=1236890#p1236890
song_list_format = "{$5%a $9}{$2%t$9}|{$7%f$9}$7$R{%b }{$3%4l$9}$9
song_status_format = "{{%a{ \"%b\"{ (%y)}} - }{%t}}|{%f}"
song_library_format = "{%N - }{%t}|{%f}"
now_playing_prefix = "$b"
alternative_header_first_line_format = "$b$8$aqqu$/a$9 $2{%t}|{%f}$9 $8$atqq$/a$9$/b"
alternative_header_second_line_format = "$b$8$aqqu$/a$9 $3{%b}|{%f}$9 $8$atqq$/a$9$/b"
song_columns_list_format = "(4)[blue]{n:#} (25)[blue]{a} (40)[blue]{t|f} (30)[blue]{b} (7f)[blue] {l}"
playlist_show_remaining_time = "yes"
playlist_separate_albums = "yes"
playlist_display_mode = "columns" (classic/columns)
browser_display_mode = "columns" (classic/columns)
search_engine_display_mode = "columns" (classic/columns)
progressbar_look = "✠❱•"
default_place_to_search_in = "database"
user_interface = "alternative"
header_visibility = "no"
titles_visibility = "yes"
show_hidden_files_in_local_browser = "no"
screen_switcher_mode = "browser, search_engine"
startup_screen = "search_engine"
ask_before_clearing_playlists = "yes"
clock_display_seconds = "yes"
display_volume_level = "yes"
display_bitrate = "no"
display_remaining_time = "yes"
regular_expressions = "extended"
ignore_leading_the = "yes"
mouse_support = "no"
enable_window_title = "yes"
allow_for_physical_item_deletion = "no"
external_editor = "vim"
use_console_editor = "yes"
colors_enabled = "yes"
header_window_color = "default"
main_window_color = "white"
progressbar_color = "green"
alternative_ui_separator_color = "red"
active_column_color = "red"
or maybe this is something that could get you what you are after.
#!/bin/bash
SESSION=ncmpcpp
#url=https://bbs.archlinux.org/viewtopic.php?pid=1167179#p1167179
tmux -2 has-session -t $SESSION
if [ $? -eq 0 ]; then
tmux -2 attach -t $SESSION
exit 0;
fi
tmux -2 new-session -d -s $SESSION 'ncmpcpp -s playlist'
for i in "C-l" "Enter" "2"
do tmux -2 send-keys -t $SESSION:0.0 "$i"
done
tmux -2 set -t $SESSION -g status off
tmux -2 attach-session -t $SESSION
I thought it was impossible, but there was something I missed.
tmux new-session -d
In order for ncmpcpp to work, the size of the actual session needed to be specified. This is what was messing with the values. To fix it, I did this:
tmux new-session -d -x 252 -y 29
It worked well after that.
Here's the final script:
#!/bin/sh
printf '\033[8;29;252t'
tmux new-session -d -x 252 -y 29
tmux select-layout even-horizontal
tmux split-window -h
tmux select-layout even-horizontal
tmux split-window -h
tmux select-layout even-horizontal
tmux send-keys -t 0 'ncmpcpp' Enter
tmux send-keys -t 1 'ncmpcpp' Enter
tmux send-keys -t 2 'ncmpcpp' Enter
tmux send-keys -t 1 '='
tmux send-keys -t 2 '8'
tmux a #

Tmux not responding to commands in shell script

I have a simple bash script that creates a new tmux session and does some layout:
#!/usr/local/bin/bash
tmux new-session -s $1
tmux split-window -h -p 50 -t 1
tmux new-window
tmux split-window -h -p 50 -t 1
The contents are in an executable script. When i execute the script with the name of the session as the argument, I get a new tmux session but there is only one un-split window, instead of the two [split] windows that I am telling it to create. If I run the scripts one by one on the shell prompt then I do get the desired outcome. So why is this not working in the script?
The problem is that first command starts tmux and wait for it to finish before continue. What you need to do is write custom tmux.conf file and add pass it through -f filename.conf with first command.
the other possible way is use tmux -d
#!/bin/bash
tmux new-session -d -s $1
tmux split-window -h -p 50 -t $1
tmux new-window -t $1
tmux split-window -h -p 50 -t $1
tmux attach -t $1

Bash script for tmux managing

I'd like to start tmux from terminal, split it window and execute in the splits some commands. Furthermore, I'd like to did with bash script, that I start from terminal. Now, I've been able to start tmux only.
#!/bin/sh
tmux
# How to split tmux window there, and start commands in splits?
There is a script started tmux, splitted for to panes and started ls in the left pane and top in the right one.
#!/bin/sh
tmux new-session -d -s main ;
tmux split-window -h ;
tmux select-pane -L
tmux send-keys -t main 'ls' C-m
tmux select-pane -R
tmux send-keys -t main 'top' C-m
tmux attach-session -d -t main

Tmux create window failed: index in use: 0

I have a script like so that I'd like to create a tmux session with windows with various server connections:
tmux new-session -d -s server-connections
tmux new-window -t server-connections:0 -n 't-u14-nickpl' 'ssh T-U14-NickPL'
tmux new-window -t server-connections:1 -n 't-u12-dev1' 'ssh T-U12-Dev1'
tmux attach -t server-connections
When I run that file, I get create window failed: index in use: 0. At first I thought maybe the script was executing so quickly it attached to the window at index 0 faster than the command could be run, so I introduced a sleep just to be sure.
tmux new-session -d -s server-connections
tmux new-window -t server-connections:0 -n 't-u14-nickpl' 'ssh T-U14-NickPL'
tmux new-window -t server-connections:1 -n 't-u12-dev1' 'ssh T-U12-Dev1'
sleep 4
tmux attach -t server-connections
But still I get create window failed: index in use: 0 and then the sleep would happen.
What do I need to change to bind to that window at index 0?
chepner's answer is correct, but you can also avoid specifying window numbers by appending windows with the -a option:
tmux new-window -a -t server-connections -n 't-u14-nickpl' 'ssh T-U14-NickPL'
tmux new-window -a -t server-connections -n 't-u12-dev1' 'ssh T-U12-Dev1'
A new session always has an initial window, so window index 0 is already taken as soon as new-session completes. Instead of an explicit new-window command, just specify the information with the new-session command itself.
tmux new-session -d -s server-connections -n 't-u14-nickpl' 'ssh T-U14-NickPL'
tmux new-window -t server-connections:1 -n 't-u12-dev1' 'ssh T-U12-Dev1'
tmux attach -t server-connections

How do I set tmux to open specified windows at startup?

How do I set up tmux so that it starts up with the specified windows opened?
You can write a small shell script that launches tmux with the required programs. I have the following in a shell script that I call dev-tmux. A dev environment:
#!/bin/sh
tmux new-session -d 'vim'
tmux split-window -v 'ipython'
tmux split-window -h
tmux new-window 'mutt'
tmux -2 attach-session -d
So everytime I want to launch my favorite dev environment I can just do
$ dev-tmux
I was trying to create a complex grid of panes and had to deal with switching and splitting panes over and over again. Here are my learnings:
tmux new-session \;
Gets you started with a new session. To split it horizontal or vertical use split-window -h or -v subsequently, like that:
tmux new-session \; split-window -v \; split-window -h \;
Creates 3 panes, like this:
------------
| |
|----------|
| | |
------------
To run commands in that panes, just add them with the send-keys 'my-command' command and C-m which executes it:
tmux new-session \; \
send-keys 'tail -f /var/log/monitor.log' C-m \; \
split-window -v \; \
split-window -h \; \
send-keys 'top' C-m \;
And the resulting session should look like that.
------------
| tail |
|----------|
| | top |
------------
Now I tried to again sub-divide the bottom left pane, so switching either back using last-pane, or in more complex windows, with the select-pane -t 1 where 1 is the number of the pane in order created starting with 0.
tmux new-session \; \
send-keys 'tail -f /var/log/monitor.log' C-m \; \
split-window -v \; \
split-window -h \; \
send-keys 'top' C-m \; \
select-pane -t 1 \; \
split-window -v \; \
send-keys 'weechat' C-m \;
Does that. Basicaly knowing your way around with split-window and select-pane is all you need. It's also handy to pass with -p 75 a percentage size of the pane created by split-window to have more control over the size of the panes.
tmux new-session \; \
send-keys 'tail -f /var/log/monitor.log' C-m \; \
split-window -v -p 75 \; \
split-window -h -p 30 \; \
send-keys 'top' C-m \; \
select-pane -t 1 \; \
split-window -v \; \
send-keys 'weechat' C-m \;
Which results in a session looking like that
------------------
| tail |
|----------------|
| | top |
|----------| |
| weechat | |
------------------
You can source different sessions from your .tmux.conf like so:
# initialize sessions
bind S source-file ~/.tmux/session1
bind s source-file ~/.tmux/session2
And then format the sessions as you require:
#session1
new -s SessionName -n WindowName Command
neww -n foo/bar foo
splitw -v -p 50 -t 0 bar
selectw -t 1
selectp -t 0
This would open 2 windows, the second of which would be named foo/bar and would be split vertically in half (50%) with foo running above bar. Focus would be in window 2 (foo/bar), top pane (foo).
You can then start your preferred tmux session (in this case, session1) with PrefixShifts
Use tmuxinator - it allows you to have multiple sessions configured, and you can choose which one to launch at any given time. You can launch commands in particular windows or panes and give titles to windows. Here is an example use with developing Django applications.
Sample config file:
# ~/.tmuxinator/project_name.yml
# you can make as many tabs as you wish...
project_name: Tmuxinator
project_root: ~/code/rails_project
socket_name: foo # Not needed. Remove to use default socket
rvm: 1.9.2#rails_project
pre: sudo /etc/rc.d/mysqld start
tabs:
- editor:
layout: main-vertical
panes:
- vim
- #empty, will just run plain bash
- top
- shell: git pull
- database: rails db
- server: rails s
- logs: tail -f logs/development.log
- console: rails c
- capistrano:
- server: ssh me#myhost
See the README at the above link for a full explanation.
:~$ tmux new-session "tmux source-file ~/session1"
session1
neww
split-window -v 'ipython'
split-window -h
new-window 'mutt'
create an alias in .bashrc
:~$ echo `alias tmux_s1='tmux new-session "tmux source-file ~/session1"'` >>~/.bashrc
:~$ . ~/.bashrc
:~$ tmux_s1
have a look # https://github.com/remiprev/teamocil
you can specify your structure using YAML
windows:
- name: sample-window
splits:
- cmd: vim
- cmd:
- ipython
width: 50
- cmd:
height: 25
This script starts a session with the name "e" and three windows
#!/bin/sh
tmux new-session -s e -n etc -d 'cd /etc; bash -i'
tmux new-window -t e:1 -n home 'cd ~; bash -i'
tmux new-window -t e:2 -n log 'cd /var/log; bash -i'
tmux select-window -t e:1
tmux -2 attach-session -t e
From my "get.all" script, which I invoke each morning to run a bunch of subsequent "get.XXX" jobs to refresh the software that I track. Some of them are auto-quitting. Others require more interaction once the get has finished (like asking to build emacs).
#!/bin/sh
tmux att -t get ||
tmux \
new -s get -n capp \; \
send-keys 'get.capp' C-m \; \
neww -n emacs \; \
send-keys 'get.emacs' C-m \; \
neww -n git \; \
send-keys 'get.git' C-m \; \
neww -n mini \; \
send-keys 'get.mini' C-m \; \
neww -n port \; \
send-keys 'get.port' C-m \; \
neww -n rakudo \; \
send-keys 'get.rakudo' C-m \; \
neww -n neil \; \
send-keys 'get.neil && get.neil2 && exit' C-m \; \
neww -n red \; \
send-keys 'get.red && exit' C-m \; \
neww -n cpan \; \
send-keys 'get.cpan && exit' C-m \; \
selectw -t emacs
If you just want to split screen on 2 panes (say horizontally), you can run this command (no tmux or shell scripts required):
tmux new-session \; split-window -h \;
You screen will look like this:
[ks#localhost ~]$ │[ks#localhost ~]$
│
│
│
│
│
│
│
│
│
│
│
[10] 0:ks#localhost:~* "localhost.localdomain" 19:51 31-янв-16
First i had the approach from #jasonwryan but if you have more then 2 configs, it can get confusing.
So i created an alias function:
tmx () {
tmux "$2" source-file "$HOME/.tmux/$1.conf";
}
In ~/.tmux/ i have multiple sessions for many uses. For example i work for different companies that have each another dev environment. So with the alias function above i can simply call: tmx company1 and load the config i need.
Update: The purpose of "$2" after the tmux command is, that you are able to pass additional tmux args.
And this is how I do it:
#!/bin/bash
function has-session {
tmux has-session -t name_of_my_session 2>/dev/null
}
if has-session ; then
echo "Session already exists"
else
cd /path/to/my/project
tmux new-session -d -s name_of_my_session 'vim'
tmux split-window -h -p 40 start_web_server
tmux split-window -v
tmux attach-session -d -t name_of_my_session
fi
I have one file for each of my project. Also you can group them to have some for work some for hobby projects.
Also you can move it to ~/bin folder, add it to PATH and give tmux_my_awesome_project name. Then you will be able to run it from each place.
You can use tmux hooks to execute commands when a new session is created. Specifically, this is achieved using the session-created hook.
For example, to split the screen three ways with top running in the lower right you can add the following line to your ~/.tmux.conf file:
set-hook -g session-created 'split -h ; split -v top'
The advantage of this method is that you don't have to run tmux in any special way (i.e., shell script or alias) to get the desired result. You can also combine this with tmux new-session -A -s mysession described in this other answer such that the hook commands are only run when you first create the session and not on subsequent attachments.
This feature was added by d15d54c2c back in 2016. The first release to include it is is 2.4.
Try bro, it is a project bootstrapper.
It provides easy apis to interact with tmux.
It looks something like this :
#!/bin/sh
# # project_name/tasks/init.sh
structure project_name
window editor
run 'vim'
window terminal
run 'echo happy coding ...'
focus editor
connect project_name
To launch a project all one needs to do is run following command -
bro start <project_name>
Checkout the screencast below, it shows how to get started with bro and use its tmux api.
https://user-images.githubusercontent.com/8296449/50532502-2cf3e400-0b6e-11e9-84af-c5f77a04e5cc.gif
There is a tmux plugin for this.
Check out tmux-resurrect
Restore tmux environment after system restart.
Tmux is great, except when you have to restart the computer. You lose all the running programs, working directories, pane layouts etc. There are helpful management tools out there, but they require initial configuration and continuous updates as your workflow evolves or you start new projects.
tmux-resurrect saves all the little details from your tmux environment so it can be completely restored after a system restart (or when you feel like it). No configuration is required. You should feel like you never quit tmux.
Or tmux-continuum
Features:
continuous saving of tmux environment
automatic tmux start when computer/server is turned on
automatic restore when tmux is started
This works for me. Creating 5 windows with the given names and auto selecting to the home window.
new -n home
neww -n emacs
neww -n puppet
neww -n haskell
neww -n ruby
selectw -t 1
smux.py allows you to simply list the commands you want in each pane, prefixed with a line containing three dashes.
Here's an example smux file that starts three panes.
---
echo "This is pane 1."
---
cd /tmp
git clone https://github.com/hq6/smux
cd smux
less smux.py
---
man tmux
If you put this in a file called Sample.smux, you can then run the following to launch.
pip3 install smux.py
smux.py Sample.smux
Full disclaimer: I am the author of smux.py.
tmuxp support JSON or YAML session configuration and a python API. A simple tmuxp configuration file to create a new session in YAML syntax is:
session_name: 2-pane-vertical
windows:
- window_name: my test window
panes:
- pwd
- pwd
The plugin tmux-continuum will auto save your tmux session and load it the next time tmux starts, should be easier to setup than some of the custom script solutions here.
Features:
continuous saving of tmux environment
automatic tmux start when computer/server is turned on
automatic restore when tmux is started
To save your session on demand you can use the tmux-resurrect plugin. tmux-resurrect is also required to run tmux-continuum
tmux-resurrect saves all the little details from your tmux environment
so it can be completely restored after a system restart (or when you
feel like it). No configuration is required. You should feel like you
never quit tmux.
To simply open tmux with multiple panes and run some commands, I created the following bash file:
#!/bin/bash
tmux split-window -v -p 30 -c ~/path/to/folder1
tmux split-window -h -p 66 -c ~/path/to/folder2
tmux split-window -h -p 50 'mongod'
Run the bash file to get the following:
-----------------------------------
| |
|---------------------------------|
| folder1 | folder2 | mongod |
-----------------------------------
I've create this script. It does not need tmuxinator, ruby or others. It is just a bash script, configurable:
A file named config should contains something like this:
combo=()
combo+=('logs' 'cd /var/log; clear; pwd')
combo+=('home' 'cd ~; clear; pwd')
and the bash code should be:
#!/bin/bash
if [ -r config ]; then
echo ""
echo "Loading custom file"
. config
else
. config.dist
fi
tmux start-server
window=0
windownumber=-1
for i in "${combo[#]}"; do
if [ $((window%2)) == 0 ]; then
name=${i}
((windownumber++))
else
command=${i}
fi
if [ ${combo[0]} == "${i}" ]; then
tmux new-session -d -s StarTmux -n "${name}"
else
if [ $((window%2)) == 0 ]; then
tmux new-window -tStarTmux:$windownumber -n "${name}"
fi
fi
if [ $((window%2)) == 1 ]; then
tmux send-keys -tStarTmux:$windownumber "${command}" C-m
fi
((window++))
done
tmux select-window -tStarTmux:0
tmux attach-session -d -tStarTmux
I just tried using all the ideas on this page and I didn't like any of them. I just wanted a solution that started tmux with a specific set of windows when my terminal opened. I also wanted it to be idempotent, i.e. opening a new terminal window takes over the tmux session from the previous one.
The above solutions often tend to open multiple tmux sessions and I want just one. First, I added this to my ~/.bash_profile:
tmux start-server
if [[ -z "$TMUX" ]]
then
exec tmux attach -d -t default
fi
then I added the following to my ~/.tmux.conf:
new -s default -n emacs /usr/local/bin/emacs
neww -n shell /usr/local/bin/bash
neww -n shell /usr/local/bin/bash
selectw -t 1
now every time I start a terminal or start tmux or whatever, I either reattach to my existing desired setup (the session named default), or create a new session with that setup.
I know my solution is quite similar to others, but I couldn't find any that had exactly what I was looking for :
I want to open titled windows at specific paths
I want to have as many windows and panes as I want
I want to run arbitrary commands in each of the panes, persisted in history
I want to focus the window I want at the end of the script
I want being able to spawn this very quickly
I want it to be easy to maintain
For my project named johndoe, I create a johndoe.conf file which is essentially a bash script, somewhere in my configs (~/.config/tmux/tmux-sessions/johndoe.conf).
This file is simple to maintain because it does not have a myriad of \ everywhere like I saw in other answers:
# Create a new session named johndoe, with a first window named Main
# at the specified starting path.
# The -d is for detached mode, which allows me to continue defining the rest of the session
# before attaching to it. Without -d, tmux would open the client right away and
# ignore the rest of the session definition
tmux new -d -s johndoe -n 'Main' -c ~/dev/own/johndoe
# Simulate the user entering some docker command in the first window available
# in the target session named (-t) johndoe
tmux send -t johndoe 'docker compose up -d' Enter
# Create a new window in the target session, with the title 'UI run'
tmux neww -t pers -n 'UI run' -c ~/dev/own/johndoe/front-end
# Simulate user entering a command to the first pane
tmux send -t pers:'UI run.0' 'git status --short' Enter
# Split this window horizontally
tmux split-window -t pers:'UI run' -h -c ~/dev/own/johndoe/front-end
# Simulate user entering a command to the second pane in this window
tmux send -t pers:'UI run.1' 'npm run dev' Enter
tmux neww -t johndoe -n 'API run' -c ~/dev/own/johndoe/back-end/Api
tmux send -t johndoe:'API run' 'dotnet run --no-build' Enter
# Focus the first window
tmux select-window -t johndoe:'Main'
# Attach the current terminal to the only session available
# (you might want to add "-t johndoe" here if you need several sessions running in parallel)
tmux a -d
I create a bash/zsh alias to source this session configuration :
alias tmuxjohndoe='. ~/.config/tmux/tmux-sessions/johndoe.conf'
I spend just the right amount of debugging time figuring out that I need to give execution permissions to the johndoe.conf file :-)
Now from anywhere, I just run tmuxjohndoe !
You should specify it in your tmux config file (~/.tmux.conf), for example:
new mocp
neww mutt
new -d
neww
neww
(opens one session with 2 windows with mocp launched in first and mutt in second, and another detached session with 3 empty windows).

Resources