GNOME 3.14+ – Launch New Gnome-Terminal and Set Title

aliasbashgnome-terminal

To the disappointment of many, tab/window title can't be set anymore with --title

I use bash. I have had a few aliases I have used to connect to remote servers with.

alias c:prod='gnome-terminal --hide-menubar --profile=Production \
--title="Production Server" -e "ssh <url>" &'

I found a workaround for GNOME 3.14+ to set title which works well in the command line once put in .bashrc

function set-title() {
  if [[ -z "$ORIG" ]]; then
    ORIG=$PS1
  fi
  TITLE="\[\e]2;$@\a\]"
  PS1=${ORIG}${TITLE}
}

However, this only seems to be effective if placed and called in the remote server's .bashrc i.e. I can only change the title after login.

It has no effect, whatsoever if I attempt to change the title of the new window before connecting:

alias c:prod='gnome-terminal --hide-menubar --profile=Production \
-e "bash -c \"source ~/.bashrc;set-title Production;ssh <url>\"" &'

Setting window title on the remote feels wrong, when the terminal is running on my box, and I cannot make it work on servers either where my user does not happen to have a home directory to put a .bashrc in.

Is there a forest I can't see for the trees?

Best Answer

  1. Append set-title function to ~/.bashrc:

    function set-title() {
      if [[ -z "$ORIG" ]]; then
        ORIG=$PS1
      fi
      TITLE="\[\e]2;$@\a\]"
      PS1=${ORIG}${TITLE}
    }
    
  2. Install expect, if you don't have it:

    sudo apt-get install expect
    
  3. Create ProductionServer.sh with content:

    #!/usr/bin/env expect
    
    spawn bash
    expect -re $ {send -- "set-title \"Production Server\"\rclear\rssh user@1.2.3.4\rclear\r"}
    interact
    exit
    
  4. Exec gnome-terminal with arguments:

    gnome-terminal --hide-menubar -e ~/ProductionServer.sh
    

Maybe that process can be optimized, but problem already solved.

Related Question