How to change the title of the mintty window?

28,702

Solution 1

What is wrong

The following command was not working for me:

echo -ne "\e]0;MYTITLE\a"

It turns out that my default Cygwin installation includes the following prompt definition in .bashrc:

PS1=\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n$

Note that the first part of the prompt (\e]0;\w\a) is setting the windows title every time the prompt appears.

The solution

Add these lines in your .bashrc that define 2 functions:

function settitle() {
      export PS1="\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n$ "
      echo -ne "\e]0;$1\a"
}
function settitlepath() {
      export PS1="\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n$ "
}

Then you can set a custom title with this command:

settitle "MYWonderfullTest here"

or you can revert to cygwin's default (the current path) with this command:

settitlepath

Hope this helps

Solution 2

You can change it with the xterm control sequence for this, like so:

echo -ne '\e]0;Title\a'

Refer to: http://code.google.com/p/mintty/issues/detail?id=241

Solution 3

Place this in .zshrc:

# Change title of MinTTY to current dir
function settitle() {
    echo -ne "\033]2;"$1"\007"
}
function chpwd() {
    settitle $(cygpath -m `pwd`)
}

The sequence of special characters in function settitle makes MinTTY change the title of the window.

In zsh, if you define a function with the special name chpwd, it will be invoked after each chdir.

Works on WinXP, with Cygwin 1.7 and MinTTY running zsh.

Solution 4

1) echo $PS1 and copy that string to your clipboard or text editor, as in
   echo $PS1
2) edit ~/.bash_profile and add shell code below, replacing $PS1 as necessary but keep the ${TERMINAL_TITLE} variable in the "false" condition.
3) Save the file and set the TERMINAL_TILE environment variable, as in
   export TERMINAL_TITLE="My Custom Title"
4) Source your bash profile, as in
   . ~/.bash_profile
Enjoy

if [ -z "${TERMINAL_TITLE}" ]
then
  PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '
else
  PS1='\[\e]0;${TERMINAL_TITLE}\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '
fi

Solution 5

In bash, the variable PROMPT_COMMAND can be set to hold a number of commands, seperated by semicolons. you can use that to do the same title setting as described in the other response that talks about zsh.

Share:
28,702

Related videos on Youtube

Leonel
Author by

Leonel

Updated on September 18, 2022

Comments

  • Leonel
    Leonel over 1 year

    MinTTY is the new default Console for Cygwin.

    What's a shell command (something I can put in .bashrc, or even better, in .zshrc) to change the title of the MinTTY window ?

    I'd like the title of the window to be the path to the current directory, and to have it updated as I switch directories inside the console.

    • eylon shabtay
      eylon shabtay over 12 years
      Cygwin's default prompt setting (i.e. $PS1) already contains a control sequence that sets the window title to user@machine:working_directory.
    • Philip Oakley
      Philip Oakley almost 8 years
      A recent MSYS2_packages/filesystem commit 6e6310d (filesystem: New specific variable MSYS2_PS1., 2016-05-01) introduced an MSYS2_PS1 prompt script, allowing distinct Cygwin/MSYS2 configurations. I've proposed a tweak to ensure that any existing PS1 has an intermediate priority github.com/Alexpux/MSYS2-packages/pull/651. Hope this helps.
    • Philip Oakley
      Philip Oakley almost 8 years
      @Philip, (note from self) That tweak has proved contentious and further tweaks are being added (or removed) to create the minimum viable fix that covers the different usages.
    • jww
      jww over 7 years
      Related, if you only need a static title like "Cygwin i686" or "Cygwin x86_64", then you can use -T <title> in the Windows shortcut properties. Also see the mintty man page.
  • voltrevo
    voltrevo over 11 years
    Has no effect for me :/ Maybe this trick has broken in the last 6 months?
  • James Fu
    James Fu about 11 years
    Are you using bash?
  • Ярослав Рахматуллин
    Ярослав Рахматуллин about 11 years
    It has no effect when issued from within a screen.
  • pabouk - Ukraine stay strong
    pabouk - Ukraine stay strong over 10 years
    Why do you define the function title if you do not use it?
  • Andrea Polci
    Andrea Polci almost 10 years
    Probably the problem is that the command prompt is already configured to update the title (for exmaple with the current path), so the command works but it is suddenly overridden by the prompt.
  • kenorb
    kenorb almost 9 years
    The question is about changing the window title, not a command prompt.
  • netawater
    netawater almost 9 years
    please add this into .bash_profile, it is OK for change title, thanks!
  • Markku K.
    Markku K. over 8 years
    Great job, addressing the PS1 problem that most users will run into with default cygwin settings.
  • Tapan Chandra
    Tapan Chandra almost 8 years
    This should have been accepted as the answer as it works perfectly unlike the highest voted answer
  • Darrel Lee
    Darrel Lee almost 8 years
    I love an answer I can just cut and paste. And also clearly explains what the problem really is.
  • Curtis Yallop
    Curtis Yallop about 7 years
    To test it "echo -ne '\e]0;Title\a' && cat". This method prevents the prompt from immediately resetting it. (If that works, you can alter PS1 in your profile as described in other answers)
  • Curtis Yallop
    Curtis Yallop about 7 years
    Alternate test: "echo -ne '\e]0;Title\a' && env -i bash --noprofile --norc".
  • hanshenrik
    hanshenrik almost 7 years
    those functions works fine for me! remember that for cygwin to reload your .bashrc, you'll have to restart cygwin, OR write source ~/.bashrc - else cygwin will still be running the old version of bashrc that was on-disk when cygwin was started. - tested on Cygwin version 2.8.1 (64 bit) - a 2017 version
  • zzxyz
    zzxyz over 6 years
    Also works in WSL (minus the cygpath stuff, of course): settitle "$(pwd)@$HOST"
  • AJ Smith 'Smugger'
    AJ Smith 'Smugger' over 6 years
    Why was this answer never accepted?
  • jdhao
    jdhao over 5 years
    If I ssh to my Linux server, the window title will change. How do I fix the window title after I start cygwin?
  • Peter Moore
    Peter Moore almost 4 years
    @kenorb This does affect the window title. in fact without setting PROMPT_COMMAND to blank you would not be able to change the window title with echo -ne "\e]0;test\a" because PROMPT_COMMAND would just revert it to user@host
  • Peter Moore
    Peter Moore almost 4 years
    @asmith I dont think that this is an answer. Im not sure why it got so many votes. without setting PROMPT_COMMAND to blank these functions will have no effect for mintty because PROMPT_COMMAND overwrites the title on its default value on linux. The answer is export PROMPT_COMMAND='echo -ne "\e]0;My desired title\a"' or export COMMAND_PROMPT='' and then set it with echo -ne "\e]0;test\a" regardless of what PS1 is set to.
  • Jordan Gee
    Jordan Gee over 2 years
    Suggesting an improvement to add (\$?) , right before the \u@\h (note the space) This gives you the exit status of the last line and it helps me sometimes.