Nested Tmux

If you don’t know the basic functionality of tmux, then visit this.

Now, we know that we can create different sessions using tmux. But what if we want the sessions open concurrently on the same window. After opening tmux, we can divide the window in several sessions. To execute things in tmux, we first have to send command prefix first and then press some key. For example, first type Ctrl-b and then press %, to divide the window into two vertical panes. You can do the similar if you want to do it horizontally. Type Ctrl-b and then (double quotes or shift+’). You can use different combination of them. To cycle between all the panes, press Ctrl-b and then o.

Now if you want to nest one tmux session into existing opened tmux session, you would type tmux again! But this doesn’t work simply. You’ll probably got something similar: “sessions should be nested with care, unset $TMUX to force“. To overcome this, you may use shortcut Ctrl-b and then type :new and press ‘Enter‘. This will open a new session window. But as they warned, sessions should be nested with care. 🙂 You can always use tmux list-sessions to know list of currently opened sessions and you can connect to them using:

tmux attach -t 2

Here 2 is the id of the session, that you’ll get from the list-session command.

Another easy way to switch between the sessions is to press Ctrl-b and then press s. Then browse the sessions and press ‘Enter’ to get attached to it.

Now I had a problem while nesting tmux sessions. Suppose you have tmux running and then you ssh into a remote machine and run tmux there. Then, while detaching the tmux session on the remote machine, your local tmux will be detached with the normal Ctrl-b and d.

So, to overcome this, you have to send two command prefixes i.e. Ctrl-b (one for the local tmux and another for talking to the remote machine’s tmux). Hence type: Ctrl-b Ctrl-b and then d to detach the tmux on ssh one.





7 thoughts on “Nested Tmux

  1. any idea why I get the error:
    “sessions should be nested with care, unset $TMUX to force”
    after starting tmux from the .bash_profile?
    when I run tmux -list-sessions there is only one
    This is while using tmux inside of WSL (windows subsystem for Linux)


    1.         Hi Barkarse,

      What do you want to achieve with this?

      To enable tmux automatically each time you open a terminal/or login?

      When you put tmux in .bash_profile and open a terminal: It opens a TTY and executes .bash_profile and runs tmux. So it works file till now. When the tmux starts (automatically), it also starts a PTS/PTY (like a TTY) and executes .bash_profile again which calls tmux again. So it’s like an infinite loop. And that’s why you are seeing that message saying “sessions should be nested with care…”.

      In easier terms:

      Opens a terminal
      -> executes .bash_profile
      –> runs tmux
      —> which again executes .bash_profile
      —-> which executes tmux. #Here you get the warning about nested tmux session because it’s running tmux inside tmux.
      —–> which executes .bash_profile
      … and so on.

      You can understand that warning well, if you manually try this:
      Open terminal:
      run: tmux
      Inside tmux, run: tmux
      It will warn you about nested session (which my original post explains about).

      You can ignore that message if it lets you do what you want. If not, have a look at Byobu tool for managing tmux.

      Let me know if you have any other doubt.



      1. Mandeep, you have explained this perfectly! I thank you for your response! The code I have in my .bash_profile I took from a few lines of code that I use to run “fortune | cowsay” where I believe it checks if they are installed and then runs the command. For tmux I wrote it out like this:

        #run TMUX with defaults
        if [ -f /usr/bin/tmux ]; then

        Since I now know what is happening to cause this error, I will research and also ask you if there is a way to implement tmux from .bash_profile with a check to see if tmux is already running, and IF NOT THEN run tmux, which should keep it from looping, correct?

        Also thank you for recommending Byobu, I will take a look at it!


        1. I did it, Checked to see if tmux was running then started it with this in .bash_profile

          if [[ -n $(pgrep tmux) ]]; then
          echo Tmux is already running
          echo Tmux is not running… starting now


          1. Great!

            You may also use:

            echo $TMUX

            to see if you are in a tmux session.

            If you are in a tmux session, it will return something like:

            $ echo $TMUX


            else, it will return nothing.

            Also, do you want to create a new terminal session each time you open a terminal? This will create a lot of tmux sessions.

            Or you can also look into “tmux windows”. I don’t know what’s your exact use case, so I can’t really suggest to use that instead.



  2. This is exactly what I was looking for. Other articles suggest doing some fancy key binding etc.
    This was fast and easy.

    Liked by 1 person

    1. Yeah, thank you! I agree.
      Glad it helped.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close