diff --git a/zsh/term_title b/zsh/term_title new file mode 100644 index 0000000..3158e39 --- /dev/null +++ b/zsh/term_title @@ -0,0 +1,43 @@ +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 + diff --git a/zshrc b/zshrc index f80f16b..f7470b9 100644 --- a/zshrc +++ b/zshrc @@ -92,3 +92,4 @@ precmd() { fi } +source ~/.zsh/term_title