0

I often start a terminal session to servers from different tabs of my terminal. I would like to create a function that before starting the session it changes the title of the tab so that I can easily recognize which tab has which session open.

Let's pretend here that my session is an ssh session. When I type in terminal

fn myserver

where fn is a function defined in ~/.bashrc:

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

the terminal starts the session and the title for the tab is changed ONLY AFTER the ssh session ends. I guess that this come because the session is open in the fn function, and only when the function returns is PS1 actualized. How to change the title/update the PS1 variable BEFORE the session begins?

aless80
  • 3,122
  • 3
  • 34
  • 53

2 Answers2

1

You are correct; your local host does not display another prompt after you add TITLE to PS1 until after ssh exits. Instead, just output TITLE immediately.

function fn() {
  set-title "$1"
  ssh $1         
}
function set-title() {
  printf '\e]2;%s\a' "$1"
}

Note that setting PS1 locally before running ssh has no effect on your prompt on the remote host anyway.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you! Could you kindly explain what this is doing? I guess printf replaces %s and prints to terminal, but I am a bit at loss about \e]2;%s\a – aless80 Jun 23 '16 at 21:12
  • It's the same text you were placing in your prompt; the `%s` is a format specification that `printf` replaces with its next argument `($1)`. It's generally good practice to do this rather than putting parameter expansions directly in the first argument to `printf`, to avoid accidentally treating embedded `%` characters as the start of a format specification. – chepner Jun 23 '16 at 21:14
  • I know about printf, and honestly I just found the \[\e]2;$*\a\] text from another forum without knowing what that exactly does. But it works. Thank you again! – aless80 Jun 24 '16 at 13:33
  • Quick overview: `\[` and `\]` are only needed in `PS1`; they tell `bash` that the characters contained within don't occupy any space when output, which helps `bash` keep track of the cursor position when editing a command line. `\e]2;...\a` is an xterm control sequence that directs the terminal to display the `...` in the title bar rather than directly on the terminal screen. – chepner Jun 24 '16 at 14:04
1

Using details from this answer, I came up with this:

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

function fn() {
  set-title $1
  ssh -t $1 "bash --init-file <(echo \"export PS1=\\\"$ORIG$TITLE\\\"\")"
}

You may have to source your .bashrc file before exporting PS1 since --init-file makes bash ignore initialization files. (I didn't need to for some reason...). Then you should replace echo \"export PS1=\\\"$ORIG$TITLE\\\"\" with echo \"cd $HOME; . .bashrc; export PS1=\\\"$ORIG$TITLE\\\"\"

Community
  • 1
  • 1
eistaa
  • 86
  • 1
  • 7