Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

gnu screen - get list of window titles

Writer Andrew Henderson

How can I obtain information (title, created time, visible content) about windows in a screen session? I need to do this in a script, so I am not looking for key bindings or interactive commands.

Something like tmux's list-windows would be great.

4 Answers

According to this thread in screen-users such a functionality has been implemented in screen. Apparently I don't have a recent enough version for it to work but this should work in recent versions of screen. [Edit] I just tried with the latest git, it work even if it's not documented in screen -h

screen -Q windows
screen -Q select my_window

The first appearance of the -Q option was in this commit.

2

Try the sequence Ctrl-a w per the instructions here.

Personally, I use a .screenrc file that keeps this information on the bottom of the screen at all times (like a Windows task bar). My screen config (~/.screenrc) looks like:

hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "
2

In GNU Screen the default key binding to get a list of all the windows (and choose one to switch to) is:

C-a "

Finally I got it!

I need to do this in a script...

Introduction

I've openned 5 windows, closed window #2 and opened a file with a problematic name 4$ esp32.pdf in window #3...

screen -Q windows
0$ man screen 1*$ user@host$ 3$ pdfread: 4$ esp32.pdf 4-$ root@server#

This string is near to unuseable!

My solution, as a function

Wanted to retrieve a correct list of open windows, even if some window title hold strings like ...2*$... or so:

Here is a function searching for existing window number, from 0 to 99 (by default).

getWinList() { if [[ $1 == -v ]];then local -n _outar="$2";shift 2 else local _outar fi _outar=() local crtnum string pointer read crtnum _ < <(screen -Q number) for ((pointer=0;pointer<=${1:-99};pointer++)) ;do read string < <(screen -Q select $pointer) [[ -z ${string%This IS*} ]] && read _outar[pointer] < <(screen -Q title) done screen -X select $crtnum [[ ${_outar[@]@A} != declare\ -a\ _outar=* ]] || for pointer in ${!_outar[@]};do printf ' %6s: %s\n' "[$pointer]" "${_outar[pointer]}" done
}

You could use -v option to store result into an array variable and add optional number of window to test for:

I've openned 5 windows, closed window #2 and opened a file with a problematic name in window 3...

getWinList [0]: man screen [1]: user@host$ [3]: pdfread: Little 4$ esp32.pdf [4]: root@server#
getWinList 3 [0]: man screen [1]: user@host$ [3]: pdfread: 4$ esp32.pdf
getWinList -v array 10
declare -p array
declare -a array=([0]="man screen" [1]="user@host\$" [3]="pdfread: 4\$ esp32.pdf" [4]="root@server#")

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy