Is there any way to detect how many levels of tmux I'm currently in? I've been able to hack it with 2 levels by simply checking [[ -n "$SSH_CLIENT" && -n "$TMUX" ]], so I can have one level on the client and one on the server. I use that check to setup a different prefix (ctrl-a, ctrl-b).

But now I'm finding I want to ssh from inside the server shell, so would like even a third level, thus requiring a third prefix combination to be setup.

Obligatory Inception meme

  • 1
    In researching this I found no methods that would allow you to do this. So you're likely going to have to hack something together.
    – slm
    Feb 23 '14 at 1:18

Apparently there is no way to check what tmux depth you are currently on. However you can easily bake your own solution. Just make a variable in your .bashrc/.bash_profile/.profile like so:

if [[ ! -n "${LC_NESTING_DEPTH}" ]]; then
    export LC_NESTING_DEPTH=1
    export LC_NESTING_DEPTH=$(expr 1 + $LC_NESTING_DEPTH)

Since the .bashrc gets loaded every time you enter tmux, the variable gets incremented two. You can even pass the variable over ssh by adding SendEnv LC_NESTING_DEPTH to the ~/.ssh/config on your machine, and AcceptEnv LC_NESTING_DEPTH in the /etc/ssh/sshd_config on the machine you're ssh-ing to (See man ssh_config and man sshd_config for more info on that). I couldn't test ssh-ing from a running ssh session, but I assume you can pass the variable on from there as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.