How to start tmux with several panes open at the same time?

Suppose I start tmux and immediately execute Ctrl+b+% and Ctrl+b+".

This gives me a tall pane on the left side of the screen; the right side of the screen has a top and bottom pane.

How can I configure tmux to start in this configuration without having to type these commands?


Solution 1:

Another option is to create an alias or another shell file in /bin for:

tmux new-session \; split-window -h \; split-window -v \; attach

or

tmux source-file ~/.tmux.conf

where ~/.tmux.conf

new
neww
splitw -h
splitw -v

For reference, same question has other options in SE, How to set up tmux so that it starts up with specified windows opened?

Solution 2:

You can use following shell script for your configuration:

#!/bin/sh 
tmux new-session -s "mySession" -d
tmux split-window -h
tmux split-window -v
tmux -2 attach-session -d 

This will give the required configuration of the screen with following commands as you mentioned. tmux --> Ctrl+b+% --> Ctrl+b+"

For reference please use tmux man page.

Solution 3:

The tmux-resurrect plugin will enable setting up session persistence as well as provide additional functionality for saving and restoring settings across tmux sessions.

Many additional features are available with this plugin. From the plugin's project page:

This plugin goes to great lengths to save and restore all the details from your tmux environment. Here's what's been taken care of:

  • all sessions, windows, panes and their order
  • current working directory for each pane
  • exact pane layouts within windows (even when zoomed)
  • active and alternative session
  • active and alternative window for each session
  • windows with focus active pane for each window
  • "grouped sessions" (useful feature when using tmux with multiple monitors) programs running within a pane! "

Installation:

  1. In the terminal (Ctrl+Alt+t), navigate to your tmux plugin directory(in my case, ~/dotfiles/tmux/plugins).
  2. Clone the repository with the command: git clone https://github.com/tmux-plugins/tmux-resurrect.

  3. Edit your .tmux.conf file and add the line set -g @plugin 'tmux-plugins/tmux-resurrect'.

  4. Reload the tmux environment with the command: tmux source-file ~/dotfiles/tmux/tmux.conf.
  5. Enter the layout that you want. In this case Ctrl-b % and Ctrl-b ".
  6. Save your tmux session by entering the command Ctrl-b + Ctrl-s.
  7. When you next start your tmux session, enter the command Ctrl-b + Ctrl-r to restore your tmux session.

As mentioned previously, in addition to setting up the pane layout of the tmux session, this plugin can also set up persistent working directories as well as have your running applications restart with each session.

Solution 4:

It can be easy to enable and disable automatic tmux sessions on login by using Byobu application. You can use Byobu as an interface to tmux to address this need, it makes it simple to do what you are asking. In a terminal, run following commands:

sudo apt-get install byobu
sudo byobu-enable
sudo -i

When the root user logs in via the console, SSH, or with sudo -i, Byobu will attach to an existing tmux session or create a new one if one is not already running. Use sudo -i instead of sudo -s. The -s option only starts a shell, not a login shell. You should use sudo -i to emulate a full login, which also loads roots ~/.profile, and this is where byobu will install itself when you run

byobu-enable.

You can configure different sessions from your .tmux.conf as below:

# initialize sessions
bind S source-file ~/.tmux/session1 
bind s source-file ~/.tmux/session2

And then you can 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).

Byobu makes setting up and starting tmux automatically very simple.

Solution 5:

I wrote myself a little bash script:

# filename tmuxv in /home/<username>/Bash/tmuxv/

#!/bin/bash
tmux new-session \; split-window -v \; rename-window ${1} \; attach

and put an alias in my ~/.bash_aliases

alias tmuxv="/home/<username>/Bash/tmuxv/tmuxv"

So now I can simply type tmuxv PYTHON and I have a vertically split tmux session with a window named PYTHON, which is nice because the window name gets reflected in my gnome-terminals tab name.