Set window title in tmux or screen to the hostname you are connecting by ssh

To set the window title in tmux or screen to the hostname you are connecting by ssh, add this to your ~/.bashrc:

settitle() {
    printf "\033k$1\033\\"
}

ssh() {
    local sshargs=("$@")
    local do_settitle=1
    tty -s || local do_settitle=0
    if [ "${TERM:0:6}" != "screen" ]; then
        local do_settitle=0
    fi
    if [ $do_settitle -eq 1 ]; then
        if [ "$1" == "-l" ]; then
            shift 2
        fi
       settitle "$*"
    fi
    command ssh "${sshargs[@]}"
    if [ $do_settitle -eq 1 ]; then
        settitle "bash"
    fi
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s