46

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
  • 861

9 Answers9

69

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,280
  • 1
  • 13
  • 21
boly38
  • 811
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,757
James Fu
  • 399
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
  • 861
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

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(){}.

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,337
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
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