7

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

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!

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.

1

Here's how I solved this in the end: by adding the following lines to my .bashrc.

_title_file=~/.title
_win_num="${TERM_SESSION_ID%%t*}"
_win_num="${_win_num#w}"
title_declare() {
  # Record title from user input, or as user argument
  [ -z "$TERM_SESSION_ID" ] && echo "Error: Not an iTerm session." && return 1
  if [ -n "$1" ]; then # warning: $@ is somehow always non-empty!
    _title="$@"
  else
    read -p "Window title (window $_win_num): " _title
  fi
  [ -z "$_title" ] && _title="window $_win_num"
  # Use gsed instead of sed, because Mac syntax is "sed -i '' <pattern> <file>" while
  # GNU syntax is "sed -i <pattern> <file>", which is annoying.
  [ ! -e "$_title_file" ] && touch "$_title_file"
  gsed -i '/^'$_win_num':.*$/d' $_title_file # remove existing title from file
  echo "$_win_num: $_title" >>$_title_file # add to file
}
title_update() {
  # Check file availability
  [ ! -r "$_title_file" ] && title_declare
  # Read from file
  _title="$(cat $_title_file | grep "^$_win_num:.*$" 2>/dev/null | cut -d: -f2-)"
  # Update or re-declare
  _title="$(echo "$_title" | sed $'s/^[ \t]*//;s/[ \t]*$//')"
  if [ -z "$_title" ]; then title_declare # reset title
  else echo -ne "\033]0;$_title\007" # re-assert existing title, in case changed
  fi
}
prompt_append() { # fix issues with consecutive semicolons
  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)
[[ ! "$PROMPT_COMMAND" =~ "title_update" ]] && prompt_append title_update
[[ "$TERM_SESSION_ID" =~ w?t?p0: ]] && [ -z "$_title" ] && title_declare

The above lines will ask the user to input a title every time 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> from within the desired window.

You must log in to answer this question.

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