0

To save one line display space, I'd like to hide the bottom status bar and put the current tmux window number in my shell prompt so it's easier to tell where I am.

GNU screen has an env var WINDOW for this, but seems like tmux does not have such a variable, so is there any way I can get the current window number in shell?

I tried TMUX_PANE but it keeps increasing, for example, when you close a window (or pane) and open a new one. I check the existence of TMUX_PANE to detect if the shell is running in a tmux window.

0

1 Answer 1

2

There is no "window number" in the terminology of tmux. There is "window index" and unique "window id". The default status line shows the former for each window, so I think you meant "window_index".

tmux display-message -p -F '#{window_index}'

Information about the window index is taken from the active pane, so the above command will be enough if you simply type it in the active pane. In general you may want a similar command that works in any pane and targets its own pane for sure:

tmux display-message -p -F '#{window_index}' -t "$TMUX_PANE"

display-message is documented here. Other #{variables} you can use are shown here.

Example PS1:

PS1="\$(tmux display-message -p -F '#{window_index}' -t \"\$TMUX_PANE\") \u@\h:\w$ "

Notes:

  • If you move a pane to another window (e.g. with tmux swap-pane …), the already printed prompts will not be updated, the old window index will be there, this may mislead you. A prompt printed after moving the pane will show the new window index.

    Your shell may or may not support a mechanism to update the current prompt (example for zsh). Updating all old prompts (still visible on screen plus those in the history of the pane) would be quite tricky and I don't expect any easy solution to exist.

  • The backslash in \$(tmux …) prevents $(…) from being evaluated when PS1 is assigned the value. It will be evaluated separately each time the prompt is about to be printed. Without the backslash PS1 would contain the window index obtained once during the assignment. Then, if you moved the pane to another window, even a newly printed prompt would show the old window index.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .