1 # AX-ZSH: Alex' Modular ZSH Configuration
2 # 10_terminal.zshrc: Initialize terminal settings
4 # Fix up TERM. Do this here (and not in zprofile), because terminal emulators
5 # often don't start a new login shell but "only" a new interactive shell.
7 # VTE based terminals (like GNOME Terminal) support 256 colors, but old(er)
8 # versions of GNOME Terminal (at least) set TERM=xterm ...
9 [[ "$TERM" = "xterm" && "$VTE_VERSION" != "" ]] && TERM="xterm-256color"
11 # Common helper functions
13 # Check if terminal supports Unicode.
14 # <https://wiki.grml.org/doku.php?id=utf8>
15 function axzsh_is_utf_terminal {
16 case "$LANG $CHARSET $LANGUAGE" in
22 alias isutfenv=axzsh_is_utf_terminal
24 # Check if terminal supports "wide" characters.
25 # <https://unix.stackexchange.com/questions/184345/detect-how-much-of-unicode-my-terminal-supports-even-through-screen>
26 function axzsh_is_widechar_terminal {
27 [[ -t 1 ]] || return 1
28 axzsh_is_utf_terminal || return 1
29 echo -ne "🍀\033[6n\033[1K\r"
32 [[ "${pos#*;}" -eq 2 ]] || return 1
36 # Test for "modern" terminal
37 function axzsh_is_modern_terminal {
38 [[ "$TERM" = screen* ]] && return 0
39 [[ "$TERM" = tmux* ]] && return 0
40 [[ "$TERM" = xterm* ]] && return 0
41 [[ "$TERM" = cygwin ]] && return 0
45 # Test for "dumb" terminal
46 function axzsh_is_dumb_terminal {
47 axzsh_is_modern_terminal && return 1
48 [[ "$TERM" = dumb* ]] && return 0
49 [[ "$TERM" = "vt52" ]] && return 0
53 # Resize terminal window (when possible)
54 function axzsh_resize_terminal {
55 printf '\e[8;%d;%dt' "$2" "$1"
60 # Set terminal "hardstatus" and "icon title"
61 function axzsh_terminal_set_icon_title {
62 [[ "$TERM" == "screen"* ]] && printf '\ek%s\e\\' "$1"
63 printf '\e]1;%s\a' "$1"
66 # Set terminal window title
67 function axzsh_terminal_set_window_title {
68 printf '\e]2;%s\a' "$1"
71 # Update terminal titles befor echoing the shell prompt
72 function axzsh_terminal_title_precmd {
73 axzsh_is_modern_terminal || return
74 axzsh_terminal_set_icon_title 'zsh'
75 if [[ "$TERM_PROGRAM" == "Apple_Terminal" && "$TERM" != "screen"* ]]; then
76 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST"
77 # Update CWD in Terminal.app
78 local url=$(echo "file://$HOSTNAME$PWD" | sed -e 's| |%20|g')
79 printf '\e]7;%s\a' "$url"
81 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST:$PWD"
85 precmd_functions+=(axzsh_terminal_title_precmd)
87 # Update terminal titles befor executing a command
88 function axzsh_terminal_title_preexec {
89 axzsh_is_modern_terminal || return
91 local cmd="${1[(w)1]}"
95 "mosh"*|"root"*|"ssh"*|"telnet"*)
99 if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
100 # Apple Terminal.app ...
101 if [[ -n "$remote" ]]; then
102 # Reset CWD for remote commands
103 printf '\e]7;%s\a' ''
106 if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
108 [[ -n "$cmd" ]] && TITLE_ADD=" – $cmd"
111 axzsh_terminal_set_icon_title "$cmd"
113 if [[ -z "$remote" ]]; then
114 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST$TITLE_ADD"
116 axzsh_terminal_set_window_title "$1"
120 preexec_functions+=(axzsh_terminal_title_preexec)
122 axzsh_is_dumb_terminal && return 0
139 no-underline "\e[24m"