Window Title in Bash

Solution 1:

Here is a nice function to do it:

# Allow the user to set the title.
function title {
   PROMPT_COMMAND="echo -ne \"\033]0;$1 (on $HOSTNAME)\007\""
}

Put that in your ~/.bashrc, then type "title whatever" to set the title. If you want to get rid of the hostname, remove "(on $HOSTNAME)".

Edit: make sure to . ~/.bashrc (aka source ~/.bashrc) before trying, of course.

Source link.

Solution 2:

I have this VT100 escape sequence defined in .bashrc.

PS1_SET_TITLE='\[\e]0;\u@\h:\w\a\]'

PS1="${PS1_SET_TITLE}" my other prompt components

export PS1

For my home directory it displays alex@host:~, when I change directories, they are updated in window title.

Works with CYGWIN and PuTTY terminal sessions. I usually don't run X, but when I did it worked fine with XTerm.

Read PROMPTING section of bash man page on available switches for PS commands, e.g \u \h \w.

Solution 3:

Wrote a function title. It supports echo-like escape sequences and -e + -E.

title () {
  echo -ne "\e]0;"
  echo -n "$@"
  echo -ne "\a"
}

I put it in my .bash folder and sourced it from .bash/global.rc.

Solution 4:

If you are using "mintty" (the default terminal of Cygwin since end 2011), add the following in .bashrc :

function title {
   export WINDOWTITLE=$1
}    
export PS1='\[\e]0;$WINDOWTITLE:\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]~\w\[\e[0m\]\n\$'

and reopen your terminal and type "title ThisIsMyTitle"