Monthly Archives: July 2017

Change window name while ssh’ing in GNU screen

When using GNU Screen everyday, it can be useful to have some automated title window renaming. I like it when I ssh a remote host. Here is how I do it with a shell function:

ssh() {
    if [[ "$(ps -p $(ps -p $$ -o ppid=) -o comm=)" =~ ^tmux ]]; then
        tmux rename-window "$(echo $* | cut -d . -f 1)"
        command ssh "$@"
        tmux set-window-option automatic-rename "on" 1>/dev/null
    elif [[ "$(ps -p $(ps -p $$ -o ppid=) -o comm=)" =~ ^screen ]]; then
        host=$(echo $* | cut -d . -f 1)
        echo -ne "\ek${host}\e\\"
        command ssh "$@"
        echo -ne "\ekzsh\e\\"
    else
        command ssh "$@"
    fi
}

The trick is to send echo -ne "\ek${host}\e\\". That also works for tmux.