Renumbering GNU Screen Windows

Mbm329 04:39, March 12, 2012 (UTC)

Occassionally your windows will become larger than 9 and when you remove some windows in the middle like windows 2 or 5 or the like, you might want to re-number them. Here's a small script I call "screnum". By running it from any window in your session, it will renumber all the windows from 0 in the current session.

It works by...
 * 1) creating a new session
 * 2) attaching with "-x" (multi-user mode)
 * 3) firing up the windowlist
 * 4) dumping a hardcopy
 * 5) parsing it
 * 6) looping through the window numbers

Thanks to Gerald Young for inspiration.


 * 1) !/bin/sh

tmpfile=/tmp/screnum.tmp

parent=$(ps -p ${PPID} -o ppid --noheaders | sed 's/\ //g') session=$(screen -ls | egrep "^blank:${parent}\." | awk '{print $1}' | cut -d. -f2-)

screen -D -m -S screnum & sleep 10 && screen -S screnum -p 0 -X height 60 screen -S screnum -p 0 -X stuff "screen -x ${session} -p = " sleep 1 && screen -S screnum -p 0 -X hardcopy ${tmpfile} screen -S screnum -p 0 -X kill

windowlist=$(awk '/^\ +[0-9]+\ / {print $1}' ${tmpfile})

if [ "${session}" = '' ] ;then echo "Can't get session name" exit fi

sleep 1

inum=0 for window in ${windowlist} ;do if [ ${window} -gt ${inum} ] ;then screen -S ${session} -p ${window} -X number ${inum} fi inum=$((${inum}+1)) done

rm ${tmpfile}