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 # Test for "modern" terminal
25 function axzsh_is_modern_terminal {
26 [[ "$TERM" = screen* ]] && return 0
27 [[ "$TERM" = tmux* ]] && return 0
28 [[ "$TERM" = xterm* ]] && return 0
32 # Test for "dumb" terminal
33 function axzsh_is_dumb_terminal {
34 axzsh_is_modern_terminal && return 1
35 [[ "$TERM" = dumb* ]] && return 0
36 [[ "$TERM" = "vt52" ]] && return 0
40 # Resize terminal window (when possible)
41 function axzsh_resize_terminal {
42 printf '\e[8;%d;%dt' "$2" "$1"
47 # Set terminal "hardstatus" and "icon title"
48 function axzsh_terminal_set_icon_title {
49 [[ "$TERM" == "screen"* ]] && printf '\ek%s\e\\' "$1"
50 printf '\e]1;%s\a' "$1"
53 # Set terminal window title
54 function axzsh_terminal_set_window_title {
55 printf '\e]2;%s\a' "$1"
58 # Update terminal titles befor echoing the shell prompt
59 function axzsh_terminal_title_precmd {
60 axzsh_is_modern_terminal || return
61 axzsh_terminal_set_icon_title 'zsh'
62 if [[ "$TERM_PROGRAM" == "Apple_Terminal" && "$TERM" != "screen"* ]]; then
63 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST"
64 # Update CWD in Terminal.app
65 local url=$(echo "file://$HOSTNAME$PWD" | sed -e 's| |%20|g')
66 printf '\e]7;%s\a' "$url"
68 axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST:$PWD"
72 precmd_functions+=(axzsh_terminal_title_precmd)
74 # Update terminal titles befor executing a command
75 function axzsh_terminal_title_preexec {
76 axzsh_is_modern_terminal || return
78 local cmd="${1[(w)1]}"
82 "mosh"*|"root"*|"ssh"*|"telnet"*)
86 if [[ "$TERM_PROGRAM" == "Apple_Terminal" ]]; then
87 # Apple Terminal.app ...
88 if [[ -n "$remote" ]]; then
89 # Reset CWD for remote commands
93 if [[ "$TERM_PROGRAM" == "iTerm.app" ]]; then
95 [[ -n "$cmd" ]] && TITLE_ADD=" – $cmd"
98 axzsh_terminal_set_icon_title "$cmd"
101 && axzsh_terminal_set_window_title "$LOGNAME@$SHORT_HOST$TITLE_ADD" \
102 || axzsh_terminal_set_window_title "$1"
105 preexec_functions+=(axzsh_terminal_title_preexec)
107 axzsh_is_dumb_terminal && return 0
124 no-underline "\e[24m"