39

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,298
  • 3
  • 16
  • 29

6 Answers6

60

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
  • 7,258
  • 1
  • 32
  • 34
19

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

Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
user2909452
  • 191
  • 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
8

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,944
  • 1
  • 21
  • 17
  • 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
  • 19,102
  • 6
  • 69
  • 67
1

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

0

I am using Gnome 42.3 (in Manjaro).

After some Google search, I was confused by the many answers so I tried to sort out what actually worked.

This works immediately in zsh:

precmd () {print -Pn "\e]0;%n@%m: %~\a"}

You can put in in ~/.zshrc.

precmd runs before every prompt, ensuring updating after cd.

This works immediately in bash:

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

You can put in in ~/.bashrc.

This feature is very useful

I have a deep file system with long directory names so if I display current directory in shell prompt then most of the terminal is filled with copies of the current directory name, so I need to have the information somewhere else and the menu bar is available therefore.

Also, I often work with many terminals, each one with different working directory, so, if the working directory is not displayed, I am lost.

Remark

I use X11, I doubt it will work with Wayland.

Pierre ALBARÈDE
  • 413
  • 4
  • 10