35

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.

  • 1
    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 8: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 github.com/Alexpux/MSYS2-packages/pull/651. Hope this helps. – Philip Oakley Jul 6 '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
  • 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 Oct 14 '16 at 7:26
50

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

  • 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 Chandra 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 7 '17 at 10:47
  • Why was this answer never accepted? – asmith Feb 13 '18 at 11:18
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

  • 4
    Has no effect for me :/ Maybe this trick has broken in the last 6 months? – voltrevo Mar 5 '13 at 23:36
  • Are you using bash? – James Fu Mar 28 '13 at 7:40
  • 2
    It has no effect when issued from within a screen. – Ярослав Рахматуллин May 3 '13 at 6:29
  • 10
    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. – Andrea Polci Jul 28 '14 at 7:47
  • 2
    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 Apr 24 '17 at 14:42
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.

  • Also works in WSL (minus the cygpath stuff, of course): settitle "$(pwd)@$HOST" – zzxyz Nov 15 '17 at 1:23
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.

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

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

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
  • Why do you define the function title if you do not use it? – pabouk Nov 12 '13 at 0:18
0

try add this into .bash_profile

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

It works for me.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.