set_termtitle() { # escape '%' chars in "${1}", make nonprintables visible local a="${(V)1//\%/\%\%}" # Truncate command, and join lines. a="${a//[$'\r'$'\n']/}" [[ "${a}" = "zsh" ]] && { a="${(%)${:-%~}}" } case "${TERM}" in screen) # plain xterm title print -rn -- $'\e'"]2;${(%)${:-%m}}: ${a}"$'\a' # screen title (in ^A") print -rn -- $'\e'"k${a}"$'\e'$'\\' # screen location print -rn -- $'\e'"_${(%)${:-%m}}: ${a}"$'\e'$'\\' ;; xterm*|rxvt*) # plain xterm title print -rn -- $'\e'"]2;${(%)${:-%m}}: ${a}"$'\a' ;; *) ;; esac } my_prompt_precmd() { set_termtitle "zsh" } my_prompt_preexec() { set_termtitle "${1}" } typeset -ga precmd_functions precmd_functions+=my_prompt_precmd typeset -ga preexec_functions preexec_functions+=my_prompt_preexec