9

I'm aware that the iTerm2 window title can be set with

echo -ne "\033]0;"Title goes here"\007"

but that appears to only set the title for a single pane. When I switch panes, the window title is changed.

How can I quickly/automatically set the window title to be the same for every pane?

3 Answers 3

5

If you are just looking for a static title you can add that line to your ~/.bash_profile. Just be sure to source it to load it:

source ~/.bash_profile

Otherwise you could use an alias. Those are also added in your ~/.bash_profile as well:

alias title1='echo -ne "\033]0;"Title goes here"\007"'
alias title2='echo -ne "\033]0;"Other Title goes here"\007"'

Hope that helps!

3

In the end I solved this by adding the following lines to my .bashrc.

# Private variables
_title_file=$HOME/.title
_win_num=${TERM_SESSION_ID%%t*}
_win_num=${_win_num#w}

# Record title from user input, or as user argument
_title_set() {  # default way is probably using Cmd-I in iTerm2
  [ -z "$TERM_SESSION_ID" ] && return 1
  if [ $# -gt 0 ]; then
    _title="$*"
  else
    read -r -p "Window title (window $_win_num):" _title
  fi
  [ -z "$_title" ] && _title="window $_win_num"
  [ -e "$_title_file" ] || touch "$_title_file"
  sed -i '/^'"$_win_num"':.*$/d' "$_title_file"  # remove existing title from file
  echo "$_win_num: $_title" >> "$_title_file"  # add to file
}

# Retrieve title from the hidden file
_title_get() {
  if [ -r "$_title_file" ]; then
    _title=$(grep "^$_win_num:.*$" "$_title_file" 2>/dev/null | cut -d: -f2-)
  else
    unset _title
  fi
  _title=$(echo "$_title" | sed $'s/^[ \t]*//;s/[ \t]*$//')
}

# Update the title
_title_update() {
  _title_get  # set _title to existing window title if possible
  [ -n "$_title" ] || _title_set  # set title name
  echo -ne "\033]0;$_title\007"  # re-assert title, in case changed
}

# Add a prompt command
_prompt() {
  export PROMPT_COMMAND=$(echo "$PROMPT_COMMAND; $1" | sed 's/;[ \t]*;/;/g;s/^[ \t]*;//g')
}

# Ask for a title when we create pane 0 (i.e. the first pane of a new window)
alias title='_title_set'  # easier for user
[[ "$PROMPT_COMMAND" =~ "_title_update" ]] || _prompt _title_update
[[ "$TERM_SESSION_ID" =~ w?t?p0: ]] && _title_update

The above lines will ask you to input a title when a new window is created (with the default as "window n"), and updates the title every time a prompt is generated. You can manually change the window title by calling title <new_title> inside the desired window.

1
  • Wow, pretty impressively comprehensive answer! Aug 20 at 4:28
1

If you go to iTerm2/Preferences/Appearance there is an area on the right side of the window where you can define names and what appears on the tabs or window. You should be able to play with these settings to get an acceptable result.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .