11

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 4

6

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
  • This seems to be the best way of going about it now. However, this has a few drawbacks in the implementation - 1. The display is limited to top few screens. 2. Invoking echo $(screen -Q windows) pauses the session as the screen's status bar is shown.
    – KalEl
    Commented Sep 13, 2016 at 17:47
  • screen -Q windows outputs depend on screen width, so could be wrong if lot of windows or small width console. Have a look at my answer Commented Aug 2, 2023 at 11:48
3

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
  • Thank you, that is nice. But as I said in the question I am not looking for a key binding. I need to get the window name and other information from a bash script.
    – sina
    Commented Aug 20, 2012 at 17:12
  • You're right, I misread your question. I'll do some research and post back if I find anything.
    – bobmagoo
    Commented Aug 20, 2012 at 17:22
3

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

C-a "
1

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! (See further, my faster function.)

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 _string _pointer _maxid=${1:-99}
    case $_maxid in
        '' | *[!0-9]* )
            cat <<EOUsage
Usage:
    $FUNCNAME [-v <varname>] [INT]
       -v varname  Populate array "$varname" with window list
       [INT]       Optional last ID to check for existence (default 99)
EOUsage
            return 1
            ;;
    esac
    for ((_pointer=0;_pointer<=_maxid;_pointer++)); do
        _string=$(screen -p $_pointer -Q title) &&
            printf -v _outar[_pointer] %s "$_string"
    done
    printf >&2 '\e[A\n\e[K'
    [[ ${_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#")

Quicker but less reliable:

As it is

  • based on output of screen -Q windows it is limited by current window width,
  • based on regular expression, any window title matching this re could break then resulting array.
getWinListFast() {
    if [[ $1 == -v ]]; then
        local -n _outar="$2";shift 2
        else local _outar
    fi
    _outar=()
    if [[ $1 ]]; then
            cat <<EOUsage
Usage:
    $FUNCNAME [-v <varname>]
       -v varname  Populate array "$varname" with window list
EOUsage
            return 1
    fi
    . <(screen -Q windows |
            sed '
                :a;
  s/^\(.*\n\|\)\([0-9]\+\)[*!-]*$ \(\([^ ]\| [^ ]\)\+\)\(  \(.*\)\|\) *$/\1[\2]="\3"\n\6/;
                ta;
                s/[|$]/\\&/g;
                s/.*/_outar=(&)\n/')
        printf >&2 '\e[A\n\e[K'
    [[ ${_outar[@]@A} != declare\ -a\ _outar=* ]] ||
        for _pointer in ${!_outar[@]};do
            printf '  %6s: %s\n' "[$_pointer]" "${_outar[_pointer]}"
        done
}

You must log in to answer this question.

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