From 50d50ff069df61b4a6120d500948fe6bf622dcf1 Mon Sep 17 00:00:00 2001 From: eeemsi Date: Thu, 15 Jun 2023 20:29:05 +0200 Subject: [PATCH] feat (zsh/term_title): add support for displaying terminal titles in a nice way --- zsh/term_title | 43 +++++++++++++++++++++++++++++++++++++++++++ zshrc | 1 + 2 files changed, 44 insertions(+) create mode 100644 zsh/term_title 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