Get current window number for bash prompt

7,238

Solution 1

You can use display-message -p to send the output to stdout to avoid having to extract it from the output of show-messages:

tmux display-message -p '#I'

You could convert to the circled numbers like this (bash syntax; tested with versions 3.2.48 and 4.2.20):

circled_digits=$(printf %s \${$'\xEA',\`,{a..s}} | iconv -f UTF-16BE)
# circled_digits='⓪①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯⑰⑱⑲⑳'

tmux_winidx_circled() {
    local winidx=$(tmux display-message -p '#I')
    if (( winidx > 20 )); then
        echo "($winidx)"
    else
        echo "${circled_digits:$winidx:1}"
    fi
}

The first line is nice in that it is pure ASCII; if you do not mind having non-ASCII characters in your configuration file, then you can go with the second line instead (it certainly makes the intent more obvious, assuming your editor and shell can properly handle the file’s encoding).

Then you could use it like this:

PS1="$(tmux_winidx_circled) \W➤ "

Solution 2

All I needed was to flash and extract the tmux message when starting the shell and store this. D-oh!

tmux display-message
export TMUX_WINDOW=$(tmux show-messages | tail -1 | grep -o '\[[0-9]\] \([0-9]\)' | grep -o '[0-9]$')
PS1="$(echo $TMUX_WINDOW) \W➤ "

Now my prompt looks like:

[2] ~➤  

and what's left is to improve parsing and substitute regular digits with pretty Unicode circled digit characters - but that's another story.

Share:
7,238

Related videos on Youtube

Trajan Unger
Author by

Trajan Unger

Updated on September 18, 2022

Comments

  • Trajan Unger
    Trajan Unger over 1 year

    I want to replace status bar with bash prompt displaying window number like ①. How can I get current window number in the shell?

    There's TMUX_PANE env variable, unfortunately no TMUX_WINDOW.

    The closest I got is: tmux display-message to flash window/pane info, then take last line of tmux show-messages and parse it. I don't want the window/pane info to flash each time the prompt is displayed.