8

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?

4

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

In the end I solved this 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
  # Use GNU sed installed via homebrew here
  [ -z "$TERM_SESSION_ID" ] && echo "Error: Not an iTerm session." && return 1
  [ $# -gt 0 ] && _title=$* || read -p "Window $_win_num title: " _title
  [ -z "$_title" ] && _title="window $_win_num"
  [ -e "$_title_file" ] || touch "$_title_file"
  gsed -i '/^'$_win_num':.*$/d' "$_title_file"  # remove existing title
  echo "$_win_num: $_title" >> "$_title_file"  # add new title
}
title_update() {
  # Read title from file and apply to window title
  [ -r "$_title_file" ] || title_declare
  _title=$(cat $_title_file | grep "^$_win_num:.*$" 2>/dev/null | cut -d: -f2-)
  _title=$(echo "$_title" | sed $'s/^[ \t]*//;s/[ \t]*$//')
  [ -z "$_title" ] && title_declare || echo -ne "\033]0;$_title\007"
}
prompt_append() {
  export PROMPT_COMMAND=$(echo "$PROMPT_COMMAND; $1" | sed 's/;[ \t]*;/;/g;s/^[ \t]*;//g')  # remove consecutive semicolons
}

# 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 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.

You must log in to answer this question.

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