32

I want to change the title of the gnome-terminal window to reflect the current directory. Is there a way to do that? A script may be? The "change terminal title" threads did not help much. Manually when we use the command:

gnome-terminal --title=$PWD 

it works, but a new terminal instance is created (as expected). I want to be able to use the

--title=$PWD 

with the cd command.

Is there a way to achieve that?

okkhoy
  • 1,148
  • 2
  • 12
  • 25

5 Answers5

54

since gnome-terminal uses the same control commands as xterm this page might be helpful.

Xterm Title for bash and other shells

TL;DR:

add following to your .bashrc

PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'

dwalter
  • 6,560
  • 1
  • 27
  • 34
17

PROMPT_COMMAND='echo -ne "\033]0;$(basename ${PWD})\007"' will display only the current directory as the title

Tamil Selvan C
  • 18,342
  • 12
  • 44
  • 63
user2909452
  • 171
  • 1
  • 2
  • 1
    Great! Any way to show parent directory & current? eg "Downloads/docs" – Marius Jul 13 '17 at 08:40
  • 4
    @MariusAndreiana Ugly and inelegant but works: `echo -ne "\033]0;$(basename $(dirname ${PWD}))/$(basename ${PWD})\007"` – vityav Jul 28 '17 at 14:48
7

I'm doing it like this to override the cd command and set pwd into the title:

function title { echo -en "\033]2;$1\007"; }
function cd { dir=$1; if [ -z "$dir" ]; then dir=~; fi; builtin cd "$dir" && title `pwd`; }
cd `pwd`

I just threw this into my ~/.bash_aliases. You could probably tweak this to do what you want.

Stoutie
  • 1,684
  • 20
  • 15
  • 1
    This works perfectly for me on MacOS 10.10 using iTerm, after adding the above functions to my `.profile`. I would also suggest adding `pushd` and `popd` as follows: `function pushd { dir=$1; if [ -z "$dir" ]; then dir=~; fi; builtin pushd "$dir" && title \`pwd\`; }` `function popd { builtin popd && title \`pwd\`; }` – ps.pf Sep 02 '15 at 10:15
  • I have a gbt prompt, and other ways to set the title were actually messing with the gbt, this saves my time, great alternative. – 0xc0de Mar 24 '20 at 07:48
1

I'm not an expert but you should try to edit your ~/.bashrc file. If I understood your problem correctly you can change your .bashrc according to my (I'm using Ubuntu 12.04). The "old" line is commented out and the new one is below it (with additional comment).

case "$TERM" in
xterm*|rxvt*)
  # OLD PS1 directive
    #PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
  # NEW PS1 directive, shows only current directory name as terminal window name
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\W\a\]$PS1"
    ;;
*)
    ;;
esac

The result is that my terminal title is equal to my current directory title eg. after cd Documents/projects my terminal title is projects (if file is open the terminal name is its name)

jmarceli
  • 15,970
  • 5
  • 57
  • 57
0

Updated Answer For White Space Problem by 'basename'

PROMPT_COMMAND='echo -ne "\033]0;$(basename "$(pwd)")\007"'

Upadate for answered Oct 23 '13 at 1:47 user2909452