Recently, I started using tmux. I find it nice, but I'm still having issues understanding this application. One of the basic questions I have is: How do I know (from the command line) what is the name of the tmux session I'm logged to? If I'm logged to some tmux session, it will tell me its name. But if I'm not logged to a tmux session, it will print either nothing or some sort of an error.
3 Answers
The name of the session is stored in the tmux variable #S, to access it in a terminal, you can do
tmux display-message -p "#S"
If you want to use it in .tmux.conf, it's simply #S. Note that the -p option will print the message on stdout, otherwise the message is displayed in the tmux status line.
If the above command is called inside a session, it returns the name of the session. If it is called outside any session, it still returns the name of the last still running session. I couldn't find a tmux command to check, if one is inside a session or not, so I had to come up with this work around:
tmux list-sessions | sed -n '/(attached)/s/:.*//p'
tmux list-sessions shows all sessions, if one is attached, it shows (attached) at the end. With sed we suppress all output (option -n) except where we find the keyword (attached), at this line we cut away everyhing after a :, which leaves us with the name of the session. This works for me inside and outside a session, as opposed to tmux display-message -p "#S".
Of course this works only if there is no : and no (attached) in the name of the session.
As commented by Chris Johnsen, a way to check if one is inside a tmux session is to see if its environment variable is set:
[[ -n "${TMUX+set}" ]] && tmux display-message -p "#S"
-
1Even it looks correct, I have to admit it didn't work for me. I have two consoles, in one I have the
tmuxsession and in the other I'm outside thetmuxsession. When I type in both of them the commandtmux display-message -p "#S", the same result is printed and the return value of both commands is the same.e271p314– e271p3142014-01-29 13:33:48 +00:00Commented Jan 29, 2014 at 13:33 -
1I see the same behavior. Look at my updated answer for a work around.pfnuesel– pfnuesel2014-01-29 17:51:06 +00:00Commented Jan 29, 2014 at 17:51
-
5You can probably just get away with checking for the TMUX environment variable (it is normally set for processes inside a session, and not set outside a session—this is not “fool proof”, since something might set or unset TMUX and confuse things, but it should be fairly reliable). In ksh, bash, zsh, et cetera:
[[ -n "${TMUX+set}" ]] && tmux display-message -p "#S"Chris Johnsen– Chris Johnsen2014-01-30 07:40:11 +00:00Commented Jan 30, 2014 at 7:40 -
Thanks for the comment. That is indeed a more elegant way and robust enough, I reckon. I added your comment in my answer.pfnuesel– pfnuesel2014-01-30 11:18:49 +00:00Commented Jan 30, 2014 at 11:18
-
What if I'm inside two tmux sessions with different names at the same time? Does this work?Mihai Danila– Mihai Danila2016-08-11 03:46:40 +00:00Commented Aug 11, 2016 at 3:46
Have a look here, using this post I understood that the one thing which distinguishes the tmux clients is the their tty but session names are tmux server property and is the same when we look at the session name from inside or outside a tmux client. Next, we may use tmux info | head which displays the the pid and tty of the client and the pid of the server.
tmux info | grep $$ | grep -q `ps -p $$ -o tty=`
The following command return value is 0 if we are inside a tmux client or non zero if we are running outside a tmux client. If you are running it from a script, make sure you use ppid instead of $$ since the tty is associated with the shell process not the script you are running.
One easy way but less robust to know if we are inside or outside tmux client is by looking at the TERM environment variable. It appears that outside the client it says 'xterm' but inside it says 'screen'. However, I find the first way much better.
Here's a perl script; making it a shell script or shell script function is an exercise left to the reader.
#!/opt/local/bin/perl
use strict;
use warnings;
if (exists $ENV{'TMUX'}) {
# session ID is the last element
# /tmp/1s/ffkjhk76sdgn/T/tmux-771/default,16772,8
my($id) = $ENV{'TMUX'};
$id =~ s{^.*,(\d+)$}{$1};
# get session name
my($cmd) = q(tmux ls -F "#{session_name}: #{session_id}");
open(IN, "$cmd |") || die qq([ERROR] Cannot open pipe from "$cmd" - $!\n);
while (<IN>) {
chomp;
if (m{(^.*):\s+\$$id$}) {
print "Session name: <$1>\n";
exit 0;
}
}
print "Unable to determine TMUX session name\n";
exit 1;
}
else {
print "Not in a TMUX session\n";
exit 1;
}
This produces the following output in a TMUX session:
<~> $ tmuxsessionname
Session name: <1>
<~> $ tmux rename-session "foobar"
<~> $ tmuxsessionname
Session name: <foobar>
Outside of a TMUX session it would produce the following:
<~> $ tmuxsessionname
Not in a TMUX session
You can ditch the print statements and just use the exit code if you want to use this in a shell script or something.