Display Git’s and Current Directory on Terminal Bar


I typically have more than ten windows open on my desktop and rely on their names to select them. Being a command-line aficionado, most of them are terminals. I have them configured to display the current directory by setting the bash PROMPT_COMMAND environment variable to 'printf "\033]0;%s:%s\007" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'. The problem is that the directory I’m often in has a generic name, such as src or doc, so the terminal’s name isn’t very useful.

I solved this with a script that displays the name of the directory holding the Git repository of the directory I’m in. I do most of my work in projects I maintain under Git, so now, as you can see in the screen dump, my terminals typically get a far more descriptive title. Example of the terminal-title script in action

Here is the script I’m using.

# Set the terminal title to the name of the Git directory (if availabe)
# and the current directory.
# Diomidis Spinellis, August 2018

# Set the terminal title to the specified argument
  printf "\033]0;%s\007" "$1"

# Current directory is a Git directory
if [ -d .git ] ; then
  set_title "${PWD##*/}"
  exit 0

# Loop to top until a Git directory is found
while : ; do
  cd ..
  if [ -d .git ] ; then
    set_title "${PWD##*/}...$CURRENT_DIR"
    exit 0
  # Root directory?
  if [ . -ef .. ] ; then
    set_title "$CURRENT_DIR"

All you need to do in order to use it is to save the script in an executable file named terminal-title and to set in your .bashrc file PROMPT_COMMAND=terminal-title.

Comments   Toot! Share

Last modified: Thursday, August 10, 2017 7:51 pm

Creative Commons Licence BY NC

Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.