Setting current ipaddress in xterm title

I wanted to display the ipaddress in the terminal title in this format: user@ipaddress /current/directory. I’m using bash, so this is how I solved it.

I set an xterm escape sequence to set the title in PROMPT_COMMAND. Bash executes the content of PROMPT_COMMAND just before displaying the prompt. In .bashrc in my home directory, I replaced the row where the title was set previously (you can see the row I replaced, I saved it in a comment). I change this on all my Linux machines, to get the ipaddress in the terminal title when I ssh to them.

See this snippet from .bashrc:

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    #PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    PROMPT_COMMAND='echo -ne "\033]0;${USER}@`hostname -I` ${PWD/$HOME/~}\007"'
;;
*)
    ;;
esac

The only drawback might be that the original code, which adds the escape sequence in the PS1 prompt, uses the \w prompt expansion to display the directory. \w expands the home directory to ~, while my code displays the the full home directory.

Read more about it in: How to change the title of an xterm http://tldp.org/HOWTO/Xterm-Title.html

Comments are closed.