Ecco come ho risolto questo alla fine: aggiungendo le seguenti righe al mio .bashrc
.
_title_file=~/.title
_win_num="${TERM_SESSION_ID%%t*}"
_win_num="${_win_num#w}"
function 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
}
function 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 "3]0;$_title_title_file=~/.title
_win_num="${TERM_SESSION_ID%%t*}"
_win_num="${_win_num#w}"
function 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
}
function 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 "3]0;$_title%pre%7" # re-assert existing title, in case changed
fi
}
function 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
7" # re-assert existing title, in case changed
fi
}
function 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
Le righe sopra chiedono all'utente di inserire un titolo ogni volta che viene creata una nuova finestra (con l'impostazione predefinita "finestra n") e aggiorna il titolo ogni volta che viene generato un prompt. Puoi modificare manualmente il titolo della finestra chiamando title <new_title>
dalla finestra desiderata.