How to change the title of the mintty window?

36

13

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.

Leonel

Posted 2011-11-28T15:38:25.473

Reputation: 743

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 https://github.com/Alexpux/MSYS2-packages/pull/651. Hope this helps.

– Philip Oakley – 2016-07-06T13:38:18.793

@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. – Philip Oakley – 2016-07-17T10:21:10.780

1

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.

– jww – 2016-10-14T07:26:47.783

1Cygwin's default prompt setting (i.e. $PS1) already contains a control sequence that sets the window title to user@machine:working_directory. – ak2 – 2011-11-29T08:50:19.443

Answers

51

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

boly38

Posted 2011-11-28T15:38:25.473

Reputation: 631

1Great job, addressing the PS1 problem that most users will run into with default cygwin settings. – Markku K. – 2016-01-27T19:13:03.230

3This should have been accepted as the answer as it works perfectly unlike the highest voted answer – Tapan Chandra – 2016-07-14T14:40:28.543

1I love an answer I can just cut and paste. And also clearly explains what the problem really is. – Darrel Lee – 2016-07-19T10:29:05.880

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 – hanshenrik – 2017-07-07T10:47:41.897

Why was this answer never accepted? – asmith – 2018-02-13T11:18:16.733

If I ssh to my Linux server, the window title will change. How do I fix the window title after I start cygwin? – jdhao – 2018-12-07T04:11:44.820

26

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

James Fu

Posted 2011-11-28T15:38:25.473

Reputation: 389

10Probably 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. – Andrea Polci – 2014-07-28T07:47:49.207

2To 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 – 2017-04-24T14:42:59.947

Alternate test: "echo -ne '\e]0;Title\a' && env -i bash --noprofile --norc". – Curtis Yallop – 2017-04-24T14:49:37.293

4Has no effect for me :/ Maybe this trick has broken in the last 6 months? – voltrevo – 2013-03-05T23:36:11.943

Are you using bash? – James Fu – 2013-03-28T07:40:51.097

2It has no effect when issued from within a screen. – Ярослав Рахматуллин – 2013-05-03T06:29:30.170

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.

Leonel

Posted 2011-11-28T15:38:25.473

Reputation: 743

Also works in WSL (minus the cygpath stuff, of course): settitle "$(pwd)@$HOST" – zzxyz – 2017-11-15T01:23:42.037

2

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.

humpy

Posted 2011-11-28T15:38:25.473

Reputation: 21

2

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

orbit andrews

Posted 2011-11-28T15:38:25.473

Reputation: 21

1

I used Leonel's answer, but I found the title would only flicker when doing this, which means at least the echo command works as intended. I ran zsh interactively with debug mode enabled using

zsh -xv

Changing the directory evidently invokes another function called title() after precmd() and chpwd(), effectively overriding them. So I plugged this into my .zshrc and it worked.

function settitle() {
    echo -ne "\033]2;"$1"\007"
}
function title() {
    settitle $(cygpath -m `pwd`)
}

If you prefer to use chpwd() or precmd() instead, simply disable the title function: title(){}.

user239512

Posted 2011-11-28T15:38:25.473

Reputation: 11

1

Just wanted to share my solution to this as I use mintty to execute a script.

My mintty shortcut is setup as:

C:\cygwin64\bin\mintty.exe -i /Cygwin-Terminal.ico /usr/bin/bash.exe -l -c /scripts/connect.sh

The connect.sh script that I wrote which is called in the shortcut above will prompt me for the server I want to connect to AND execute the settitle function defined in the script.

connect.sh

#!/bin/bash

echo "Enter servername when prompted"
echo -n "servername: " 
read servername
function settitle() {
    echo -ne "\033]2;"$servername"\007"
}
function title() {
    settitle $(cygpath -m `pwd`)
}
settitle
ssh my_username@$servername

user273306

Posted 2011-11-28T15:38:25.473

Reputation: 11

Why do you define the function title if you do not use it? – pabouk – 2013-11-12T00:18:53.007

0

Several users have indicated that the escape codes weren't working - so try adding a delay after the command - ( to observe that the escape codes are processed ) and then it becomes apparent that the bash prompt may be resetting the windows title

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

David Dyck

Posted 2011-11-28T15:38:25.473

Reputation: 1

0

try add this into .bash_profile

export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}:${PWD/#$HOME/~}\007"'

It works for me.

netawater

Posted 2011-11-28T15:38:25.473

Reputation: 183

2The question is about changing the window title, not a command prompt. – kenorb – 2015-07-28T10:08:40.630

1please add this into .bash_profile, it is OK for change title, thanks! – netawater – 2015-07-28T14:28:46.427