45

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
  • 851
  • 4
  • 9
  • 12
  • 3
    Cygwin's default prompt setting (i.e. `$PS1`) already contains a control sequence that sets the window title to user@machine:working_directory. – ak2 Nov 29 '11 at 08:50
  • 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 Jul 06 '16 at 13:38
  • @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 Jul 17 '16 at 10:21
  • 2
    Related, if you only need a static title like *"Cygwin i686"* or *"Cygwin x86_64"*, then you can use `-T ` in the Windows shortcut properties. Also see the [`mintty man page`](http://mintty.github.io/mintty.1.html). – jww Oct 14 '16 at 07:26

9 Answers9

66

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

ndemou
  • 1,050
  • 1
  • 10
  • 18
boly38
  • 781
  • 5
  • 3
  • 1
    Great job, addressing the PS1 problem that most users will run into with default cygwin settings. – Markku K. Jan 27 '16 at 19:13
  • 3
    This should have been accepted as the answer as it works perfectly unlike the highest voted answer – Tapan Nallan Jul 14 '16 at 14:40
  • 1
    I love an answer I can just cut and paste. And also clearly explains what the problem really is. – Darrel Lee Jul 19 '16 at 10:29
  • 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 Jul 07 '17 at 10:47
  • Why was this answer never accepted? – AJ Smith 'Smugger' Feb 13 '18 at 11:18
  • If I ssh to my Linux server, the window title will change. How do I fix the window title after I start cygwin? – jdhao Dec 07 '18 at 04:11
  • @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. – Peter Moore Aug 01 '20 at 06:35
  • 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. – Jordan Gee Oct 20 '21 at 19:14
27

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

sblair
  • 12,617
  • 6
  • 48
  • 77
James Fu
  • 399
  • 3
  • 5
4

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
  • 851
  • 4
  • 9
  • 12
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
  • 21
  • 2
2

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
  • 21
  • 1
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
1

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
zx485
  • 2,170
  • 11
  • 17
  • 24
David Dyck
  • 61
  • 4
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
  • 11
  • 1
1

try add this into .bash_profile

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

It works for me.

netawater
  • 203
  • 1
  • 5
  • 2
    The question is about changing the window title, not a command prompt. – kenorb Jul 28 '15 at 10:08
  • 1
    please add this into .bash_profile, it is OK for change title, thanks! – netawater Jul 28 '15 at 14:28
  • @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 Aug 01 '20 at 05:55