37

I'm writing a script. I really want to know this. I hope tmux executable can tell me this.

I find

tmux rename <newname>

can rename current session. But I can't find a command to fetch its own name.

52

With tmux 1.2 (and later), you can use the -p option of display-message to output a message to stdout (instead of displaying it to an attached client):

tmux display-message -p '#S'

#S is formatted as the session name (see the description of the status-left option in the man page).

| improve this answer | |
  • +1, much better than my answer. – chepner Apr 9 '12 at 12:52
  • I really appreciate that you point out it require tmux 1.2, which is exactly the version available for openSUSE 11.3 auto online update. – Jimm Chen Apr 10 '12 at 1:19
  • With tmux 1.8, this returns the name of an attached session even when I'm not working in that session. – Mihai Danila Aug 11 '16 at 3:44
  • Tim People's answer below works with tmux 1.8 and later, as well as addresses the points he raises. That is the better answer. – Alexandros Nov 26 '18 at 11:52
11

I'm surprised that, after nearly 5 years, no one has pointed out that neither of these answers is adequate. While both work fine as long as the current TTY is attached to the only tmux session on the host, these answers both fall flat if:

  1. the current terminal session is not part of a tmux session, or
  2. there are multiple, attached tmux sessions

In the former case, both answers here report back the name of the attached session (regardless of whether the current tty is governed by that session). In the latter case, the result is likely indeterminate or will result in multiple answers.

The proper question should be, "What is the name of the tmux session to which my current terminal session is attached?"

For he answer to that question, execute:

for s in $(tmux list-sessions -F '#{session_name}'); do
    tmux list-panes -F '#{pane_tty} #{session_name}' -t "$s"
done | grep "$(tty)" | awk '{print $2}'

This works regardless of the number of tmux sessions (attached or otherwise) and regardless of whether the current terminal session is or is not part of a tmux session.

| improve this answer | |
  • 1
    Great answer. Rather than looping through each session name to list its panes, you can also use tmux list-panes -a -F '#{pane_tty} #{session_name}' to list every session's panes. Not sure how new -a is, so YMMV. – Brian Cline Jun 3 '17 at 22:12
  • Came back to this question after using accepted answer and failing as per issues described here. Please upvote this. If possible the accepted answer should be changed. – Alexandros Nov 26 '18 at 11:39
  • 2
    Method 1 works fine if combined with checking TMUX environment variable prior to running tmux display-message -p '#S' for a typical desktop setup. – ZeroPhase Apr 28 '19 at 8:50
9

Extended/corrected tim-peoples’s answer as per don_crissti’s comment on Why is this grep -v not functioning as expected?.

"$(tty)" command in tim-peoples’s answer

| grep "$(tty)" |

would not work in that context as expected. It evaluates to a string 'not a tty'.

Replacing it with a variable solves this problem.

tty=$(tty)

...

| grep "$tty" |

Also, when no tmux sessions exist, the original code would produce

"no server running on /tmp/tmux-1000/default" error message.

Add 2>/dev/null and the code would run without printing out the error message.

Modified code reads as:

tty=$(tty)
for s in $(tmux list-sessions -F '#{session_name}' 2>/dev/null); do
    tmux list-panes -F '#{pane_tty} #{session_name}' -t "$s"
done | grep "$tty" | awk '{print $2}'
| improve this answer | |
  • 1
    What's the improvement? If it's a necessary one you should consider submitting it as an edit rather than an answer. To make it a good answer you probably should consider including some more descriptive text. – Seth Apr 24 '17 at 12:21
  • 1
    @seth, I wanted to comment but oddly didn't have enough credit to do so. – bright side Apr 24 '17 at 13:52
  • You need 50 reputation to comment. It's usually not a good idea to comment with complex code as you lack line breaks and so on. Your edit made it a pretty good answer IMHO. – Seth Apr 25 '17 at 5:12
  • Oh, I get it now. The "tty" issue is a bash thing. I don't use that toy shell :D – Tim Peoples Nov 6 '18 at 18:57
  • 1
    @TimPeoples While it might be a Bash issue. Bash is still the standard to code shell scripts against for compatibility on modern terminals. Sure, the posix standard is ideal; but, bash has some features all of the more advanced terminals share. By hitting Bash most of the other major terminals should just work. I just run shell check and catch a bunch of bad errors and formatting issues. (mostly using old syntax in place of current more descriptive syntax) – ZeroPhase Apr 28 '19 at 6:45
5
  • Chris Johnsen's answer returns a session name even when querying from outside of one.
  • bright side's and Tim Peoples' answer assume a tty.

Instead, I found this to work accurately for me:

if [[ -n "$TMUX_PANE" ]]; then
    session_name=$(tmux list-panes -t "$TMUX_PANE" -F '#S' | head -n1)
fi
| improve this answer | |
3

Not ideal, but you can extract the name of the attached session with awk:

tmux list-sessions -F '#{session_name} #{session_attached,yes,}' | awk '$2=="yes" {print $1}'

The custom format is a little easier to parse than the default.

| improve this answer | |

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.