- The shebang is
/bin/sh, no need for Bash here. - Get used to double-quoting variables. It's way better to habitually quote regardless if you need it than forget to quote when it makes a real difference.
- I used lowercase names for variables.
- I used
tputto determine the size of the current terminal. Thennew-session -d -x … -y …uses proper values. But: - In my initial tests this seemed unnecessary, all worked without
-xor-y… - … although according to my local manual it should be necessary.
- Anyway if the session already exists, its size is determined and
new-session -d -x … -y …will have no impact. You may experience distorted proportions in the layout after you attach and the panes get resized. There are tmux commands and options that may help (resize-window,window-size,default-size), although not in my oldish tmux, so I won't elaborate. tputoperandslinesandcolsare not required by POSIX. See this answer for alternatives.- If window cannot be created, the script exits.
- Each
split-windowtargets the right window. If the session existed or someone attached in the meantime and changed the window, the current window would not be what you assume. To be even more robust eachsplit-windowshould target a respective pane explicitly. Note thatselect-panebeforesplit-window(or beforeresize-paneas in your script) is not really robust in a general setup where many clients can interact with the same tmux server: some other client (user or script) could select another pane between these two commands. - I do not resize panes. I create them with proper percentages in the first place. The
-poption I use works in my oldish tmux. The documentation tells me the newer syntax is likesplit-window -l 20%andresize-panecan work with percentages as well. I don't know if the newest tmux understandssplit-window -p. The documentation doesn't mention this option any more, it may be deprecated and unsupported or it may be kept for compatibility; I cannot test this (feedback or edit is welcome). split-windowtakes percentages that refer to the available space, i.e. to the old size of the pane that is about to shrink to make space for the new pane (at least this is how it works in my oldish tmux with-p). This is why numbers like22appear ( 20/(20+70) ≈ 22 ). According to the documentationresize-paneuses percentages of the window size.- I use or don't use
-dto stay in the current pane or make the new one current, depending on which pane I need to target with the nextsplit-window. - When I need to specify/select a pane that fits a token like
{top-right}, I use the token. - I do not use
send-keysto run commands. In general do not usesend-keysto run commands. Any(?) tmux command that creates a new shell inside tmux can run a shell command instead. In the example scriptecho "step 0"; bashis such shell command. It includesbashat the end, so the pane doesn't exit when the actual command (echo) finishes. An alternative is theremain-on-exitoption. Your shell commands (watch) can work indefinitely, so you may not need such tricks. Running a shell command withsend-keyslike you do in your script makes sense if the command affects the shell it runs in and your goal is to work interactively in the shell prepared this way. E.g. the command may set variables or source a script; or you want to be able to interruptwatchand have it in the shell history. In any other case the "right" way is to pass commands withsplit-window,respawn-paneor some similar tmux command.
- The shebang is
/bin/sh, no need for Bash here. - Get used to double-quoting variables. It's way better to habitually quote regardless if you need it than forget to quote when it makes a real difference.
- I used lowercase names for variables.
- If window cannot be created, the script exits.
- Each
split-windowtargets the right window. If the session existed or someone attached in the meantime and changed the window, the current window would not be what you assume. To be even more robust eachsplit-windowshould target a respective pane explicitly. Note thatselect-panebeforesplit-window(or beforeresize-paneas in your script) is not really robust in a general setup where many clients can interact with the same tmux server: some other client (user or script) could select another pane between these two commands. - I do not resize panes. I create them with proper percentages in the first place. The
-poption I use works in my oldish tmux. The documentation tells me the newer syntax is likesplit-window -l 20%andresize-panecan work with percentages as well. I don't know if the newest tmux understandssplit-window -p. The documentation doesn't mention this option any more, it may be deprecated and unsupported or it may be kept for compatibility; I cannot test this (feedback or edit is welcome). split-windowtakes percentages that refer to the available space, i.e. to the old size of the pane that is about to shrink to make space for the new pane (at least this is how it works in my oldish tmux with-p). This is why numbers like22appear ( 20/(20+70) ≈ 22 ). According to the documentationresize-paneuses percentages of the window size.- I use or don't use
-dto stay in the current pane or make the new one current, depending on which pane I need to target with the nextsplit-window. - When I need to specify/select a pane that fits a token like
{top-right}, I use the token. - I do not use
send-keysto run commands. In general do not usesend-keysto run commands. Any(?) tmux command that creates a new shell inside tmux can run a shell command instead. In the example scriptecho "step 0"; bashis such shell command. It includesbashat the end, so the pane doesn't exit when the actual command (echo) finishes. An alternative is theremain-on-exitoption. Your shell commands (watch) can work indefinitely, so you may not need such tricks. Running a shell command withsend-keyslike you do in your script makes sense if the command affects the shell it runs in and your goal is to work interactively in the shell prepared this way. E.g. the command may set variables or source a script; or you want to be able to interruptwatchand have it in the shell history. In any other case the "right" way is to pass commands withsplit-window,respawn-paneor some similar tmux command.
- The shebang is
/bin/sh, no need for Bash here. - Get used to double-quoting variables. It's way better to habitually quote regardless if you need it than forget to quote when it makes a real difference.
- I used lowercase names for variables.
- I used
tputto determine the size of the current terminal. Thennew-session -d -x … -y …uses proper values. But: - In my initial tests this seemed unnecessary, all worked without
-xor-y… - … although according to my local manual it should be necessary.
- Anyway if the session already exists, its size is determined and
new-session -d -x … -y …will have no impact. You may experience distorted proportions in the layout after you attach and the panes get resized. There are tmux commands and options that may help (resize-window,window-size,default-size), although not in my oldish tmux, so I won't elaborate. tputoperandslinesandcolsare not required by POSIX. See this answer for alternatives.- If window cannot be created, the script exits.
- Each
split-windowtargets the right window. If the session existed or someone attached in the meantime and changed the window, the current window would not be what you assume. To be even more robust eachsplit-windowshould target a respective pane explicitly. Note thatselect-panebeforesplit-window(or beforeresize-paneas in your script) is not really robust in a general setup where many clients can interact with the same tmux server: some other client (user or script) could select another pane between these two commands. - I do not resize panes. I create them with proper percentages in the first place. The
-poption I use works in my oldish tmux. The documentation tells me the newer syntax is likesplit-window -l 20%andresize-panecan work with percentages as well. I don't know if the newest tmux understandssplit-window -p. The documentation doesn't mention this option any more, it may be deprecated and unsupported or it may be kept for compatibility; I cannot test this (feedback or edit is welcome). split-windowtakes percentages that refer to the available space, i.e. to the old size of the pane that is about to shrink to make space for the new pane (at least this is how it works in my oldish tmux with-p). This is why numbers like22appear ( 20/(20+70) ≈ 22 ). According to the documentationresize-paneuses percentages of the window size.- I use or don't use
-dto stay in the current pane or make the new one current, depending on which pane I need to target with the nextsplit-window. - When I need to specify/select a pane that fits a token like
{top-right}, I use the token. - I do not use
send-keysto run commands. In general do not usesend-keysto run commands. Any(?) tmux command that creates a new shell inside tmux can run a shell command instead. In the example scriptecho "step 0"; bashis such shell command. It includesbashat the end, so the pane doesn't exit when the actual command (echo) finishes. An alternative is theremain-on-exitoption. Your shell commands (watch) can work indefinitely, so you may not need such tricks. Running a shell command withsend-keyslike you do in your script makes sense if the command affects the shell it runs in and your goal is to work interactively in the shell prepared this way. E.g. the command may set variables or source a script; or you want to be able to interruptwatchand have it in the shell history. In any other case the "right" way is to pass commands withsplit-window,respawn-paneor some similar tmux command.
#!/bin/sh
session="$USER"
window="$session:1"
lines="$(tput lines)"
columns="$(tput cols)"
tmux -2 new-session -d -x "$columns" -y "$lines" -s "$session" 'echo "step -1"; bash'
tmux new-window -t "$window" -n 'Logs' 'echo "step 0"; bash' || exit
tmux split-window -t "$window" -h -p 67 -d 'echo "step 1"; bash'
tmux split-window -t "$window" -v -p 10 -b -d 'echo "step 2a"; bash'
tmux split-window -t "$window" -v -p 22 -d 'echo "step 2b"; bash'
tmux split-window -t "$window" -h -p 50 'echo "step 3"; bash'
tmux split-window -t "$window.{top-right}" -v -p 55 -d
tmux split-window -t "$window.{top-right}" -v -p 64
tmux select-pane -t "$window.{bottom-right}"
tmux split-window -t "$window" -v -p 67
tmux split-window -t "$window" -v -p 50
tmux select-window -t "$window"
# Attach to session
tmux -2 attach-session -t "$session"
#!/bin/sh
session="$USER"
window="$session:1"
tmux -2 new-session -d -s "$session" 'echo "step -1"; bash'
tmux new-window -t "$window" -n 'Logs' 'echo "step 0"; bash' || exit
tmux split-window -t "$window" -h -p 67 -d 'echo "step 1"; bash'
tmux split-window -t "$window" -v -p 10 -b -d 'echo "step 2a"; bash'
tmux split-window -t "$window" -v -p 22 -d 'echo "step 2b"; bash'
tmux split-window -t "$window" -h -p 50 'echo "step 3"; bash'
tmux split-window -t "$window.{top-right}" -v -p 55 -d
tmux split-window -t "$window.{top-right}" -v -p 64
tmux select-pane -t "$window.{bottom-right}"
tmux split-window -t "$window" -v -p 67
tmux split-window -t "$window" -v -p 50
tmux select-window -t "$window"
# Attach to session
tmux -2 attach-session -t "$session"
#!/bin/sh
session="$USER"
window="$session:1"
lines="$(tput lines)"
columns="$(tput cols)"
tmux -2 new-session -d -x "$columns" -y "$lines" -s "$session" 'echo "step -1"; bash'
tmux new-window -t "$window" -n 'Logs' 'echo "step 0"; bash' || exit
tmux split-window -t "$window" -h -p 67 -d 'echo "step 1"; bash'
tmux split-window -t "$window" -v -p 10 -b -d 'echo "step 2a"; bash'
tmux split-window -t "$window" -v -p 22 -d 'echo "step 2b"; bash'
tmux split-window -t "$window" -h -p 50 'echo "step 3"; bash'
tmux split-window -t "$window.{top-right}" -v -p 55 -d
tmux split-window -t "$window.{top-right}" -v -p 64
tmux select-pane -t "$window.{bottom-right}"
tmux split-window -t "$window" -v -p 67
tmux split-window -t "$window" -v -p 50
tmux select-window -t "$window"
# Attach to session
tmux -2 attach-session -t "$session"
One way to learn what the order may be is to draw the layout on a piece of paper and cut it with scissors with straight cuts that go through, so each cut divides the current piece into two smaller ones. Or imagine breaking a chocolate bar.
A possible scheme for your desired layout is like this:
- The shebang is
/bin/sh, no need for Bash here. - Get used to double-quoting variables. It's way better to habitually quote regardless if you need it than forget to quote when it makes a real difference.
- I used lowercase names for variables.
- If window cannot be created, the script exits.
- Each
split-windowtargets the right window. If the session existed or someone attached in the meantime and changed the window, the current window would not be what you assume. To be even more robust eachsplit-windowshould target a respective pane explicitly. Note thatselect-panebeforesplit-window(or beforeresize-paneas in your script) is not really robust in a general setup where many clients can interact with the same tmux server: some other client (user or script) could select another pane between these two commands. - I do not resize panes. I create them with proper percentages in the first place. The
-poption I use works in my oldish tmux. The documentation tells me the newer syntax is likesplit-window -l 20%andresize-panecan work with percentages as well. I don't know if the newest tmux understandssplit-window -p. The documentation doesn't mention this option any more, it may be deprecated and unsupported or it may be kept for compatibility; I cannot test this (feedback or edit is welcome). split-windowtakes percentages that refer to the available space, i.e. to the old size of the pane that is about to shrink to make space for the new pane (at least this is how it works in my oldish tmux with-p). This is why numbers like22appear ( 20/(20+70) ≈ 22 ). According to the documentationresize-paneuses percentages of the window size.- I use or don't use
-dto stay in the current pane or make the new one current, depending on which pane I need to target with the nextsplit-window. - When I need to specify/select a pane that fits a token like
{top-right}, I use the token. - I do not use
send-keysto run commands. In general do not usesend-keysto run commands. Any(?) tmux command that creates a new shell inside tmux can run a shell command instead. In the example scriptecho "step 0"; bashis such shell command. It includesbashat the end, so the pane doesn't exit when the actual command (echo) finishes. An alternative is theremain-on-exitoption. Your shell commands (watch) can work indefinitely, so you may not need such tricks. Running a shell command withsend-keyslike you do in your script makes sense if the command affects the shell it runs in and your goal is to work interactively in the shell prepared this way. E.g. the command may set variables or source a script; or you want to be able to interruptwatchand have it in the shell history. In any other case the "right" way is to pass commands withsplit-window,respawn-paneor some similar tmux command.
One way to learn what the order may be is to draw the layout on a piece of paper and cut it with scissors with straight cuts that go through, so each cut divides the current piece into two smaller ones. A possible scheme for your desired layout is like this:
- The shebang is
/bin/sh, no need for Bash here. - Get used to double-quoting variables. It's way better to habitually quote regardless if you need it than forget to quote when it makes a real difference.
- I used lowercase names for variables.
- If window cannot be created, the script exits.
- Each
split-windowtargets the right window. If the session existed or someone attached in the meantime and changed the window, the current window would not be what you assume. To be even more robust eachsplit-windowshould target a respective pane explicitly. Note thatselect-panebeforesplit-window(or beforeresize-paneas in your script) is not really robust in a general setup where many clients can interact with the same tmux server: some other client (user or script) could select another pane between these two commands. - I do not resize panes. I create them with proper percentages in the first place. The
-poption I use works in my oldish tmux. The documentation tells me the newer syntax is likesplit-window -l 20%andresize-panecan work with percentages as well. I don't know if the newest tmux understandssplit-window -p. The documentation doesn't mention this option any more, it may be deprecated or it may be kept for compatibility; I cannot test this (feedback or edit is welcome). split-windowtakes percentages that refer to the available space, i.e. to the old size of the pane that is about to shrink to make space for the new pane (at least this is how it works in my oldish tmux with-p). This is why numbers like22appear ( 20/(20+70) ≈ 22 ). According to the documentationresize-paneuses percentages of the window size.- I use or don't use
-dto stay in the current pane or make the new one current, depending on which pane I need to target with the nextsplit-window. - When I need to specify/select a pane that fits a token like
{top-right}, I use the token. - I do not use
send-keysto run commands. In general do not usesend-keysto run commands. Any(?) tmux command that creates a new shell inside tmux can run a shell command instead. In the example scriptecho "step 0"; bashis such shell command. It includesbashat the end, so the pane doesn't exit when the actual command (echo) finishes. An alternative is theremain-on-exitoption. Your shell commands (watch) can work indefinitely, so you may not need such tricks. Running a shell command withsend-keyslike you do in your script makes sense if the command affects the shell it runs in and your goal is to work interactively in the shell prepared this way. E.g. the command may set variables or source a script; or you want to be able to interruptwatchand have it in the shell history. In any other case the "right" way is to pass commands withsplit-window,respawn-paneor some similar tmux command.
One way to learn what the order may be is to draw the layout on a piece of paper and cut it with scissors with straight cuts that go through, so each cut divides the current piece into two smaller ones. Or imagine breaking a chocolate bar.
A possible scheme for your desired layout is like this:
- The shebang is
/bin/sh, no need for Bash here. - Get used to double-quoting variables. It's way better to habitually quote regardless if you need it than forget to quote when it makes a real difference.
- I used lowercase names for variables.
- If window cannot be created, the script exits.
- Each
split-windowtargets the right window. If the session existed or someone attached in the meantime and changed the window, the current window would not be what you assume. To be even more robust eachsplit-windowshould target a respective pane explicitly. Note thatselect-panebeforesplit-window(or beforeresize-paneas in your script) is not really robust in a general setup where many clients can interact with the same tmux server: some other client (user or script) could select another pane between these two commands. - I do not resize panes. I create them with proper percentages in the first place. The
-poption I use works in my oldish tmux. The documentation tells me the newer syntax is likesplit-window -l 20%andresize-panecan work with percentages as well. I don't know if the newest tmux understandssplit-window -p. The documentation doesn't mention this option any more, it may be deprecated and unsupported or it may be kept for compatibility; I cannot test this (feedback or edit is welcome). split-windowtakes percentages that refer to the available space, i.e. to the old size of the pane that is about to shrink to make space for the new pane (at least this is how it works in my oldish tmux with-p). This is why numbers like22appear ( 20/(20+70) ≈ 22 ). According to the documentationresize-paneuses percentages of the window size.- I use or don't use
-dto stay in the current pane or make the new one current, depending on which pane I need to target with the nextsplit-window. - When I need to specify/select a pane that fits a token like
{top-right}, I use the token. - I do not use
send-keysto run commands. In general do not usesend-keysto run commands. Any(?) tmux command that creates a new shell inside tmux can run a shell command instead. In the example scriptecho "step 0"; bashis such shell command. It includesbashat the end, so the pane doesn't exit when the actual command (echo) finishes. An alternative is theremain-on-exitoption. Your shell commands (watch) can work indefinitely, so you may not need such tricks. Running a shell command withsend-keyslike you do in your script makes sense if the command affects the shell it runs in and your goal is to work interactively in the shell prepared this way. E.g. the command may set variables or source a script; or you want to be able to interruptwatchand have it in the shell history. In any other case the "right" way is to pass commands withsplit-window,respawn-paneor some similar tmux command.