Change gnome-terminal title to reflect the current directory?
Asked Answered
N

6

42

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?

Nolan answered 9/5, 2012 at 13:27 Comment(0)
L
64

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"'

Lobbyism answered 9/5, 2012 at 13:35 Comment(2)
strip ${USER}@${HOSTNAME}: if you just want the pwdLobbyism
To only see the last two directories inside the path: echo -ne "\033]0;${PWD/${PWD%*\/*\/*}\/}\007"Macropterous
A
22

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

Attune answered 23/10, 2013 at 1:47 Comment(2)
Great! Any way to show parent directory & current? eg "Downloads/docs"Ardatharde
@MariusAndreiana Ugly and inelegant but works: echo -ne "\033]0;$(basename $(dirname ${PWD}))/$(basename ${PWD})\007"Contain
P
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.

Platy answered 1/8, 2012 at 16:36 Comment(2)
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`; }Kmeson
I have a gbt prompt, and other ways to set the title were actually messing with the gbt, this saves my time, great alternative.Janessajanet
K
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)

Kylstra answered 3/11, 2013 at 19:20 Comment(0)
Y
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

Yarak answered 8/12, 2020 at 17:14 Comment(0)
P
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.

Panegyrize answered 14/2, 2023 at 15:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.