Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
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
up vote 0 down vote accepted

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
else
    export LC_NESTING_DEPTH=$(expr 1 + $LC_NESTING_DEPTH)
fi

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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