Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new tmux session from inside a tmux session

Tags:

tmux

People also ask

How do I create a new tmux session from your current tmux session?

Using the Prefixes to Control Tmux By default, the prefix is CTRL+B. That is, we have to press the keys CTRL+B and then the command. For example, to create a new session, the command would be C. So, to create a new session we need to press CTRL+B and next C – CTRL+B, C.

How do I open multiple tmux sessions?

To create multiple windows, you need at least one tmux session running. You can simply type CTRL + b, let go of both keys and type 'c'. This will open up a new terminal. At the bottom, you can see that there are now multiple windows (0, 1, 2) in the session.

How do you split tmux?

ctrl + b + " to make a Horizontal split. ctrl + b + left arrow to move to the left pane. ctrl + b + " to make a Horizontal split.


The quickest way (assuming you use ctrl-b as your command prefix) is:

ctrl-b :new

To create a new session, then

ctrl-b s

to interactively select and attach to the session.


How to create the script

This script will check if a session exists. If session does not exist create new session and attach to it. If session does exist nothing happens and we attach to that session. Feel free to replace `~/development' with project name.

$ touch ~/development && chmod +x ~/development

# ~/development

tmux has-session -t development
if [ $? != 0 ]
then
  tmux new-session -s development
fi
tmux attach -t development  

New session from terminal

Let's create two detached sessions, list them, attach to one and then from within tmux cycle through sessions.

tmux new -s name -d works from inside tmux because we're creating a new detached session. Otherwise you'll get a nesting error.

$ tmux new -s development -d
$ tmux new -s foo -d
$ tmux ls
> development: 1 windows (created Wed Jan 13 11:31:38 2016) [204x54]
> foo: 1 windows (created Wed Jan 13 11:31:38 2016) [204x54]
$ tmux attach -t
$ tmux ls
> development: 1 windows (created Wed Jan 13 11:31:38 2016) [204x54] (attached)
> foo: 1 windows (created Wed Jan 13 11:31:38 2016) [204x54]

New session from within tmux

We are now inside or better known as attached to our target session. If we try to create a new session while attached it will result in a nesting error.

$ tmux new -s bar
> sessions should be nested with care, unset $TMUX to force

To solve this we create a new detached session. e.g.,

$ tmux new -s bar -d
$ tmux ls
> development: 1 windows (created Wed Jan 13 11:31:38 2016) [204x54] (attached)
> foo: 1 windows (created Wed Jan 13 11:31:38 2016) [204x54]
> bar: 1 windows (created Wed Jan 13 17:19:35 2016) [204x54]

Cycle (switch) Sessions

  • Prefix ( previous session
  • Prefix ) next session

note: Prefix is Ctrl-bby default. You can bind Prefix to Ctrl-a and in Mac OSX you can change Caps Lock to ctrl system preferences > keyboard > modifier keys

Attach to a session using command mode while inside tmux

Trying to attach to a session without detaching will result in an error.

$ tmux attach -t development
> sessions should be nested with care, unset $TMUX to force

Instead use command mode Prefix : then type attach -t session_name and hit enter.


Using this works for me:

TMUX= tmux new-session -d -s name
tmux switch-client -t name

The TMUX= on the first line is required so tmux doesn't throw a sessions should be nested with care, unset $TMUX to force message.


All the commands you can launch within your terminal, like tmux new -s sessionName can be launched from within tmux by pressing the trigger key (eg: ctrl-b) then : then the command without the starting tmux part.

As a result, ctrl-b : followed by new -s sessionName will do exactly what you want and give a name to your session. It also switches automatically to the new session.