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.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityWith 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).
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:
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.
tmux list-panes -a -F '#{pane_tty} #{session_name}'
to list every session's panes. Not sure how new -a
is, so YMMV.
Jun 3, 2017 at 22:12
TMUX
environment variable prior to running tmux display-message -p '#S'
for a typical desktop setup.
Apr 28, 2019 at 8:50
-f
filters which will let you shorted this to a single command. Not sure what version added #{==:...}
formats that let you turn -F
output conditional on a match to arbitrary strings, but that can be used to only output the session name for the current tty as well, but then you have to post-process to remove the blank lines.
Jun 17, 2022 at 20:54
tmux list-panes -a -F "#{?#{==:#{pane_tty},$(tty)},#{session_name},}" | grep '.'
. That's #{?<test>,<true>,<false>}
on the outside to conditionally change the output, the test uses #{==:<value1>,<value2>}
to test if two strings are equal, testing #{pane_tty}
against $(tty)
(interpolating the current tty string), and if that's true, #{session_name}
is output, otherwise nothing.
Jun 17, 2022 at 21:00
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}'
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
session_name
if $TMUX_PANE is not even set.
| head -n1
shouldn't be needed since only one row (or no rows) can be returned.
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.