I want to write a script that gets the active tmux window's name and uses it as a variable for my vim session. Is this possible? I looked through the tmux manual and didn't see anything.
3 Answers
You can use display-message -p
to query the name of the active window (among other things):
tmux display-message -p '#W'
If you want to target a specific window, you can use -t
:
tmux -t «target-window» display-message -p '#W'
See the man page for the various ways to specify a target window (search for “target-window” in the Commands section).

- 214,407
- 26
- 209
- 186
SYNOPSIS tmux [-28lquvVC] [-c shell-command] [-f file] [-L socket-name] [-S socket-path] [command [flags]]
SKIP
command [flags] This specifies one of a set of commands used to control tmux, as described in the following sections. If no commands are specified, the new-session command is assumed.
You can find full list of tmux commands (two last arguments) in the manual, but now you interest is 'list-windows'.
tmux list-windows
0: zsh [156x40] [layout aebd,156x40,0,0,0] @0
1: mc [156x40] [layout aebe,156x40,0,0,1] @1 (active)
As you can see active window marked as '(active)'. This is what you were looking for?

- 113,384
- 42
- 163
- 163

- 481
- 5
- 15
First list all window names and active status, the active one will end with 1, then extract the name before it
tmux lsw -F '#{window_name}#{window_active}'|sed -n 's|^\(.*\)1$|\1|p'

- 973
- 1
- 12
- 18