How to automatically set terminal title to directory name without path

Solution 1:

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).

Solution 2:

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.