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?
36 Answers
Another option is to create an alias or another shell file in /bin for:
tmux new-session \; split-window -h \; split-window -v \; attachor
tmux source-file ~/.tmux.confwhere ~/.tmux.conf
new
neww
splitw -h
splitw -vFor reference, same question has other options in SE, How to set up tmux so that it starts up with specified windows opened?
1You 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.
1The 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:
- In the terminal (Ctrl+Alt+t), navigate to your tmux plugin directory(in my case,
~/dotfiles/tmux/plugins). Clone the repository with the command:
git clone.Edit your
.tmux.conffile and add the lineset -g @plugin 'tmux-plugins/tmux-resurrect'.- Reload the tmux environment with the command:
tmux source-file ~/dotfiles/tmux/tmux.conf. - Enter the layout that you want. In this case Ctrl-b % and Ctrl-b ".
- Save your tmux session by entering the command Ctrl-b + Ctrl-s.
- 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.
1It 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 -iWhen 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/session2And 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 0This 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.
2Here 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="~/projects" SESSION="work" 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 "cd $WORK_DIR && vim notes" Enter tmux send-keys -t $SESSION:1.1 "G" Enter tmux send-keys -t $SESSION:1.3 "cd $WORK_DIR && ll" Enter tmux attach-session -t $SESSION
} 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} \; attachand 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.