12

Currently, my bash terminal/tab title is set based on the directory I'm in. For example:

bob@bobscomputer:~/i/had/some/great/pie/yesterday

This isn't very useful if I have multiple tabs open side-by-side. For example, if I have 4 tabs of the above dir open side-by-side, all I see is

bob@bobscomputer:~/i/had...

4 times.

I'd like all my terminal titles to be set automatically to just the last part of the path. In the example above, I'd like the title to be

yesterday

Obviously, the title should change when I change directories. I'd like to do this without changing my prompt, so the prompt and the terminal title should be different.

Is there a way to do this?

1

2 Answers 2

13

Why do you want to do it without modifying your PS1 variable? That is the correct way to do it.

If you run the following, you'll get what you want:

export PS1="\[\e]0;\W\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "

This is just taking the default prompt:

export PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "

... and replacing \u@\h: \w with \W.

You can do the same thing with any command that echoes the right escape code:

echo -en "\e]0;${PWD##*/}\a"

... but then you'd be constantly fighting bash to stop it from doing it using PS1 (the best way).

2
  • 2
    Thanks - I hadn't realized PS1 sets the title and prompt independently. In my .bashrc, under case "$TERM" in xterm*|rxvt*), I set PS1="[\e]0;${debian_chroot:+($debian_chroot)}\W\a]$PS1", and it does exactly what I wanted. Apr 4, 2014 at 13:55
  • This question does a good job of explaining what all that PS1 gobbledygook means: askubuntu.com/questions/404341/… Oct 31, 2014 at 1:16
4

Yes. Prepend: \[\e]0;\W\a\] to your PS1. This will not change your prompts appearance, but will automatically set your terminal tabs title to the basename of the current working directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .