48

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?

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83

6 Answers6

43

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?

user.dz
  • 48,105
  • 4
    A note to others, the above answer only works inside a pre-existing tmux session. To execute this from a generic terminal session perform the following command: tmux new-session -s <SESSION_NAME> "tmux source-file '$~/.tmux.conf'". You should alias this command to a bash function for convenience. Your tmux.conf should contain the content listed in the answer above. – seeker Jun 16 '20 at 00:00
23

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.

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
Harshit
  • 415
  • unfortunately the ~/.bashrc does not seems to be executed in all panes, I lost my aliases with this solution. – рüффп Mar 17 '20 at 14:57
6

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.

Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
5

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.

  • 1
    why bind S vs bind s? (first large S, then small s?) – knb Sep 13 '17 at 07:40
  • @knb bind in this case is a tmux configuration command. He's just creating two tmux commands that are bound to s and then S. You can choose other combinations if you like. Doing this allows you to effectively run the tmux commands specified in the files by key combinations while inside a tmux session – sherrellbc May 01 '20 at 12:16
3

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.

Wu Wei
  • 183
  • 8
  • 1
    Good answer, but please remove the question at the end. Try asking it as a new question, which will make sure more people see it. – Tom Brossman Oct 30 '18 at 11:57
3

Here is a .bash_aliases function that opens up a tall pane on the left and splits the right pane vertically (see picture below).

"-t $SESSION:1.2" indicates you want to execute the command in the "work" session, in the first window, in the second pane (<prefix + q> lists pane numbers).

"G" is the vim command that takes you to the end of the file.

openSession () {
WORK_DIR=&quot;~/projects&quot;
SESSION=&quot;work&quot;

tmux kill-session -t $SESSION
tmux new-session -d -s $SESSION
tmux split-window -h -t $SESSION:1.1
tmux split-window -v -t $SESSION:1.2
tmux send-keys -t $SESSION:1.1 &quot;cd $WORK_DIR &amp;&amp; vim notes&quot; Enter
tmux send-keys -t $SESSION:1.1 &quot;G&quot; Enter
tmux send-keys -t $SESSION:1.3 &quot;cd $WORK_DIR &amp;&amp; ll&quot; Enter
tmux attach-session -t $SESSION

}

enter image description here

Jeff_V
  • 141
  • 5