Search code examples
gnu-screen

quickly switch between gnu screens


I would like to quickly switch between different gnu screen sessions (not windows). Right now I can detach from one session with screen -d and reattach to the next one with screen -r <sessionname>, but is there not a way to do this in one command? Something like screen -d && screen -r <sessionname>?


Solution

  • This is easy enough to do via a utility script:

    #!/usr/bin/env bash
    
    set -eu
    set -o pipefail
    unset CDPATH
    : "${BASH:?bash shell is required}"
    
    
    screen -ls || true
    
    for s in $(screen -ls | tail -n +2 | head -n -2 | sort -R | cut -f2)
    do
        read -n 1 -s -r -p '[y/n] switch to '"$s"'?' REPLY
        if [[ "$REPLY" == "y" ]]
            then
                screen -DR "$s"
            else echo
        fi
    done
    

    Save that on your path as cycle-screen and make it executable. Usage example:

    There are screens on:
        43094.27    (Detached)
        47415.12    (Attached)
        54922.31    (Detached)
    3 Sockets in /var/folders/r5/7_prvssx301dyz4jz2pbky5w0000gn/T/.screen.
    
    [y/n] switch to 54922.31?
    

    Type y to enter the screen named in the prompt, or anything else to skip over it. When you leave the screen (via ^a^d, ^d, exit or whatever) you get the prompt for the subsequent screen.