Bash: Setting the title of your terminal tab

Posted Over 10 years ago. Visible to the public.

If your terminal has many tabs, you'll want to keep them organized. To change their title from the prompt, run this function:

function tab_title {
  if [ -z "$1" ]
  then
    title=${PWD##*/} # current directory
  else
    title=$1 # first param
  fi
  echo -n -e "\033]0;$title\007"
}

Put it into your ~/.bashrc to have it always available. Adjust to your needs.

Usage

$> tab_title
# title set to the current directory's name
$> tab_title new_title
# title set to "new_title"

Auto-setting the title

If you want your title to update each time you change the working directory, put this code after the function definition.

# auto-title
cd() { builtin cd "$@" && tab_title; }
pushd() { builtin pushd "$@" && tab_title; }
popd() { builtin popd "$@" && tab_title; }
tab_title
Dominik Schöler
Last edit
Over 3 years ago
Keywords
terminator
License
Source code in this card is licensed under the MIT License.
Posted by Dominik Schöler to makandra dev (2014-01-17 14:24)