How to change title of Git terminal in Windows?

13,251

Solution 1

You were on the right track with this link

If you modify the git-prompt.sh script a bit (for me, this is located in c:\Program Files (x86)\Git\etc\profile.d\git-prompt.sh), you can make the title anything you want.

Note: You will need to run VS Code, Notepad++ or similar as administrator to write back to this directory.

First, save a backup of git-prompt.sh (like git-prompt.backup.sh), then modify the start of git-prompt.sh as follows:

if test -z "$GITTITLEPREFIX" # if not empty
then
    GITTITLEPREFIX="Git-Bash => " # prefix that will have current pwd appended after it
fi

if test -f ~/.config/git/git-prompt.sh
then
    . ~/.config/git/git-prompt.sh
else
    if test -n "$GITTITLE"
    then   ##### Set window title directly to GITTITLE if not empty
        PS1='\[\033]0;$GITTITLE\007\]' 
    else   ##### Set window title to GITTITLE PREFIX plus the PWD
        PS1='\[\033]0;$GITTITLEPREFIX${PWD//]^[:ascii:]]/?}\007\]' 
    fi
fi
###### Leave the rest of the file the same
    PS1="$PS1"'\n'
    PS1="$PS1"'\[\033[32m\]'
###### Etc.

This will first check if GITTITLEPREFIX is empty, and if not, it will set it to "Git-Bash => " similar to in the linked article. This will have the current path appended after it, so if you want "1 : $PWD", then set GITTITLEPREFIX to "1 : " like this:

GITTITLEPREFIX="1 : "

Otherwise, you can set GITTITLE to any non-empty value, and then the entire title will be set exactly to the contents of GITTITLE (no PWD appended), so if you just want "1", use this:

GITTITLE="1"

Then run the script. With my path, I did it like this:

. "/c/Program Files (x86)/Git/etc/profile.d/git-prompt.sh"

and the title should change. Of course, you can alias this or make a separate script from it in a location that is in the path so running it is much simpler, and the title could just be an argument. I'll leave that as an exercise for the reader...

Solution 2

This thread is a few months old. But I think an alternative will be helpful

You can add following line to .bashrc file in your user profile folder

export TITLEPREFIX="Git Bash"

where you want Git bash to be your title prefix. This is user specific change. So if a machine is used by multiple users with their own logins, everyone can customize their own title.

Solution 3

A simple option is echo -ne "\e]0;YOUR TITLE HERE\a".

Solution 4

In JSON setting write for Git Console:

"name": "Git Bash",
"tabTitle": "Git Bash",
"suppressApplicationTitle": true

Solution 5

I solved my question making very little modifications to the script. The first one, to pass the name I want for the window, I added the variable name=$1 and set it in the title variable:

name=$1
PS1='\[\033]0;$name\007\]' # set window title

The second one, as recommended here, I commented the next lines:

#PS1="$PS1"'\[\033[35m\]'       # change to purple
#PS1="$PS1"'$MSYSTEM '          # show MSYSTEM

The final code is below:

if test -f /etc/profile.d/git-sdk.sh
then
    TITLEPREFIX=SDK-${MSYSTEM#MINGW}
else
    TITLEPREFIX=$MSYSTEM
fi
name=$1
PS1='\[\033]0;$name\007\]' # set window title
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'\[\033[32m\]'       # change to green
PS1="$PS1"'\u@\h '             # user@host<space>
#PS1="$PS1"'\[\033[35m\]'       # change to purple
#PS1="$PS1"'$MSYSTEM '          # show MSYSTEM
PS1="$PS1"'\[\033[33m\]'       # change to brownish yellow
PS1="$PS1"'\w'                 # current working directory
if test -z "$WINELOADERNOEXEC"
then
    GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
    COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
    COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
    COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
    if test -f "$COMPLETION_PATH/git-prompt.sh"
    then
        . "$COMPLETION_PATH/git-completion.bash"
        . "$COMPLETION_PATH/git-prompt.sh"
        PS1="$PS1"'\[\033[36m\]'  # change color to cyan
        PS1="$PS1"'`__git_ps1`'   # bash function
    fi
fi
PS1="$PS1"'\[\033[0m\]'        # change color
PS1="$PS1"'\n'                 # new line
PS1="$PS1"'$ '                 # prompt: always $
MSYS2_PS1="$PS1"               # for detection by MSYS2 SDK's bash.basrc

Temporally I made a copy of the script and pasted it on C:, to execute it easily every time I need to change the title, according to my path, as as follows: $ . /c/changename.sh

I'm still learning about scripting so I could be able to set an alias. As @LightCC said, "I'll leave that as an exercise for the reader..."

Share:
13,251
Isidro Serrano Pineda
Author by

Isidro Serrano Pineda

Meh... Not so much to say. Sarcastic humor. I try to get along with the rest of humans the best I can. I take deep breaths sometimes and continue my walk through life.

Updated on June 04, 2022

Comments

  • Isidro Serrano Pineda
    Isidro Serrano Pineda almost 2 years

    I work in Windows 10 and usually I have up to 5 CMD windows open. I work this way because I need to run the same application with different data and keep monitoring if any exception is thrown.

    I set a number as the window's title (using the title command) instead of the default text, in order to easily identify which window I'm working in and be able to identify and change between them using Alt+Tab (an example of how I work with my CMD windows)

    Recently I started to use Git and I really like the Git Bash terminal for Windows. I would like to work with Git Bash terminal the same way I work with CMD windows, but I can't find any way to change the window title. I searched a bit and found these instructions and some others (that I can't paste because I'm not allowed to post more than two links yet), but it seems to work only by setting a different default title. I'd like to change the window title to any custom text I choose, at any moment.

    Is this possible? Is there a command like title available for Git Bash?

  • Isidro Serrano Pineda
    Isidro Serrano Pineda over 6 years
    Works for me, not exactly a title command as I expected, but perfect for what I need, and as you say, I'll try different options using the script. Thanks a lot!
  • LightCC
    LightCC over 6 years
    Found a bug here - the first if..fi construct should be checking for empty (or running the default assignment on else rather than then). I fixed this by modifying the -n to a -z for the test flag.
  • Isidro Serrano Pineda
    Isidro Serrano Pineda over 6 years
    Uhm.. That must be why it didn't work in the first time. At the end, following your indications, I made my own modifications to the script to pass the title as a parameter. As I said, thanks!
  • LightCC
    LightCC over 6 years
    @IsidroSerranoPineda You could post your final script as another answer that could help others that are looking for the same thing.
  • Jon
    Jon about 5 years
    # title "title of Git Bash window" title() { echo -ne "\e]0;$1\a"; }
  • Fabio A. Correa
    Fabio A. Correa over 2 years
    @Robyte I beg to differ. I have been using this command for many years. I would suggest you review the definition of the PS1 environment variable in your bash initialization files.