Skip to content

Instantly share code, notes, and snippets.

@asmagill
Last active November 19, 2021 10:14
Show Gist options
  • Save asmagill/4b792359c7a01da46b2d to your computer and use it in GitHub Desktop.
Save asmagill/4b792359c7a01da46b2d to your computer and use it in GitHub Desktop.
Bash resize terminal function

I forget where I found this, so if anyone wants to claim attribution, let me know and I'll add a line here. If put into /etc/profile.d/serial-console.sh, the following will auto size a serial terminal windows size and create a function for manually adjusting later. Since I access RPIs and BBBs via the serial console, their tty names are already listed in the case statement; add others if your system is different. rsz helps when you later reconnect and minicom or screen is in a different sized window...

    rsz() {
        if [[ -t 0 && $# -eq 0 ]];then
                local IFS='[;' escape geometry x y
                echo -ne '\e7\e[r\e[999;999H\e[6n\e8'
                read -sd R escape geometry
                x=${geometry##*;} y=${geometry%%;*}
                if [[ ${COLUMNS} -eq ${x} && ${LINES} -eq ${y} ]];then
                        echo "${TERM} ${x}x${y}"
                else
                        echo "${COLUMNS}x${LINES} -> ${x}x${y}"
                        stty cols ${x} rows ${y}
                fi
        else
                print 'Usage: rsz'
        fi
    }


    case $( /usr/bin/tty ) in
            /dev/ttyAMA0|/dev/ttyO0|/dev/ttyS0) export LANG=C
                                                rsz
                                                ;;
    esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment