tmux: Find "my" window, check if it's active

I want to find some way to have a program find the following information:

1) What window am I running in?

2) Is that window the active window?

The idea is, in a program like WeeChat, I'll switch to another window and work on something. When I switch out, I'd like to run 'set_unread_current_buffer' - so when I return to that window, WeeChat will have a line after my last read messages.

There's some WeeChat plugins out there, but they find out what session WeeChat is attached to, and whether or not that session is currently attached, and sets the /away status appropriately.

I'm looking for something similar, but for when I change windows, and to just run 'set_unread_current_buffer' instead of /away


If you are running commands inside a tmux pane, then you can use the TMUX_PANE environment variable* to target the “containing” pane.

To identify the “containing” window:

tmux display-message -pt "$TMUX_PANE" '#{session_name}:#{window_index}'

Or, depending on your needs, you might like to use window_id. The man page has a list of other format variables. For example, when run inside a pane, this command

tmux display-message -pt "$TMUX_PANE" '#{window_active}'

will output 1 if the pane’s window is active or 0 otherwise.

You might use pane_active if you care whether the specific pane is active.

If you want to check on some other pane, you will need to supply your own target-pane identifier (like sess:win.0).


* tmux 1.5 and later sets TMUX_PANE for the pane to a unique value when starting its initial command; other programs run via this initial program will generally inherit this environment variable.