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 [[ -z "$AXZSH_PLUGIN_CHECK" ]] || return 1
29 axzsh_is_utf_terminal || return 1
34 [[ "${pos#*;}" -eq 2 ]] || return 1
38 # Test for "modern" terminal
39 function axzsh_is_modern_terminal {
40 [[ "$TERM" = screen* ]] && return 0
41 [[ "$TERM" = tmux* ]] && return 0
42 [[ "$TERM" = xterm* ]] && return 0
43 [[ "$TERM" = cygwin ]] && return 0
47 # Test for "dumb" terminal
48 function axzsh_is_dumb_terminal {
49 axzsh_is_modern_terminal && return 1
50 [[ "$TERM" = dumb* ]] && return 0
51 [[ "$TERM" = "vt52" ]] && return 0
55 # Resize terminal window (when possible)
56 function axzsh_resize_terminal {
57 printf '\e[8;%d;%dt' "$2" "$1"
62 # Set terminal "hardstatus" and "icon title"
63 function axzsh_terminal_set_icon_title {
64 [[ "$TERM" == "screen"* ]] && printf '\ek%s\e\\' "$1"
65 printf '\e]1;%s\a' "$1"
68 # Set terminal window title
69 function axzsh_terminal_set_window_title {
70 printf '\e]2;%s\a' "$1"
73 # Update terminal titles befor echoing the shell prompt
74 function axzsh_terminal_title_precmd {
75 axzsh_is_modern_terminal || return
76 axzsh_terminal_set_icon_title 'zsh'
77 if [[ "$TERM_PROGRAM" == "Apple_Terminal" && "$TERM" != "screen"* ]]; then
78 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST"
79 # Update CWD in Terminal.app
80 local url=$(echo "file://$HOSTNAME$PWD" | sed -e 's| |%20|g')
81 printf '\e]7;%s\a' "$url"
83 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST:$PWD"
87 precmd_functions+=(axzsh_terminal_title_precmd)
89 # Update terminal titles befor executing a command
90 function axzsh_terminal_title_preexec {
91 axzsh_is_modern_terminal || return
93 local cmd="${1[(w)1]}"
97 "mosh"*|"root"*|"ssh"*|"telnet"*)
101 if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
102 # Apple Terminal.app ...
103 if [[ -n "$remote" ]]; then
104 # Reset CWD for remote commands
105 printf '\e]7;%s\a' ''
108 if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
110 [[ -n "$cmd" ]] && TITLE_ADD=" – $cmd"
113 axzsh_terminal_set_icon_title "$cmd"
115 if [[ -z "$remote" ]]; then
116 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST$TITLE_ADD"
118 axzsh_terminal_set_window_title "$1"
122 preexec_functions+=(axzsh_terminal_title_preexec)
124 alias axttyinfo="nocorrect zsh $AXZSH/bin/axttyinfo"
126 axzsh_is_dumb_terminal && return 0
143 no-underline "\e[24m"