How to change title of Git terminal in Windows?
Asked Answered
D

9

29

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?

Dissatisfied answered 27/9, 2017 at 23:47 Comment(6)
In standard Bash you can write echo -ne "\e]0;YOUR TITLE HERE\a" - not sure if git bash is the sameRios
In Git-Bash command above sets title for 1-2 secs and then it is changed back to defaultIne
@M.M's suggestion is working well these days. Thanks!Sankhya
Doesn't work in 2022, unfortunately.Funchal
@Funchal the accepted answer may work for you, if you want to give it a try. It's still working for me nowadays. It's more cumbersome than a single line, but works.Dissatisfied
@Funchal , I just accepted a simpler answer, in case you want to check it.Dissatisfied
D
1

Here is a simple way to change the title text to anything you want. create a title.sh script somewhere on your system like in C:\bin that has the following content:

#!/bin/sh
export TITLEPREFIX="$*"

Add the following to your .bashrc file in your home directory.

alias title='. /c/bin/title.sh'

Now open a git bash window. You can now type:

title my title

and your title will start with the title you provided to the title alias.

Duchy answered 28/7, 2023 at 17:30 Comment(1)
Wow. So simple, yet so useful. Thank you, very much!Dissatisfied
S
21

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

Sankhya answered 12/1, 2019 at 18:57 Comment(2)
# title "title of Git Bash window" title() { echo -ne "\e]0;$1\a"; }Bladdernose
@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.Sankhya
S
18

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.

Sateia answered 12/2, 2018 at 6:28 Comment(1)
Now, how does one get this to work when passing an argument to git-bash.exe? For example: git-bash.exe -c "export TITLEPREFIX=\"Test\" && sleep 5" doesn't work.Kosse
F
12

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

Fanaticize answered 28/9, 2017 at 1:26 Comment(5)
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!Dissatisfied
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.Fanaticize
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!Dissatisfied
@IsidroSerranoPineda You could post your final script as another answer that could help others that are looking for the same thing.Fanaticize
Thank you very much for your answer @LightCC, I don't mean any harm to your reputation by unaccepting your answer, I just found a simpler answer and I think it deserves to be marked as answer.Dissatisfied
G
3

In JSON setting write for Git Console:

"name": "Git Bash",
"tabTitle": "Git Bash",
"suppressApplicationTitle": true
Gilder answered 12/7, 2021 at 13:9 Comment(0)
C
3

I know this is old, but if it helps someone, you can set the title prefix simply by changing the TITLEPREFIX env variable:

TITLEPREFIX=YourTitlePrefix

Carniola answered 30/3, 2023 at 11:53 Comment(1)
that works well..Pengelly
D
2

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

Dissatisfied answered 30/9, 2017 at 0:26 Comment(0)
A
2

You could use

export MYTITLE=abcd; export PS1=$(echo $PS1 | sed -r "s/(0;).*?(\\\\007)/\1$MYTITLE\2/")

It find regular expression for title and replace it with $MYTITLE. I could be wrong but I assume title is something between 0; and \007 in $PS1. It work for me.

Or you could add next function to your .bashrc (or .bash_profile)

ttt() {
        # change title in gitbash
        export PS1=$(echo $PS1 | sed -r "s/(0;).*?(\\\\007)/\1$1\2/")
}

and then anytime use ttt "new title"

Arbela answered 22/3, 2022 at 10:48 Comment(0)
D
1

This thread is old but if anyone is still looking for a solution,

  1. Terminal> Settings> Tab title>

  2. Change it to anything you would like then scroll down.

  3. Advanced> Suppress title changes> Turn on> Save

I hope this helps you!

Drunk answered 6/6, 2023 at 15:37 Comment(2)
what application is this for? mine says "mintty 3.6.4" and there is only "Options" in the window bar context menu (and funnily: copy title). it doesn't support tabs afaik, and there is no title option in the options.Tandratandy
This is specifically for Windows Terminal: learn.microsoft.com/en-us/windows/terminalBarkeeper
D
1

Here is a simple way to change the title text to anything you want. create a title.sh script somewhere on your system like in C:\bin that has the following content:

#!/bin/sh
export TITLEPREFIX="$*"

Add the following to your .bashrc file in your home directory.

alias title='. /c/bin/title.sh'

Now open a git bash window. You can now type:

title my title

and your title will start with the title you provided to the title alias.

Duchy answered 28/7, 2023 at 17:30 Comment(1)
Wow. So simple, yet so useful. Thank you, very much!Dissatisfied

© 2022 - 2024 — McMap. All rights reserved.