From da30536709f9e78076d90e2c7206f0135e62d2e8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 4 Mar 2022 10:44:32 +0530 Subject: [PATCH] bash/zsh integration: Show hostname in title when in an SSH session --- shell-integration/bash/kitty.bash | 10 +++++++--- shell-integration/zsh/kitty-integration | 19 ++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/shell-integration/bash/kitty.bash b/shell-integration/bash/kitty.bash index d8ab21a46..e676e648b 100644 --- a/shell-integration/bash/kitty.bash +++ b/shell-integration/bash/kitty.bash @@ -6,7 +6,10 @@ if [[ -z "$KITTY_SHELL_INTEGRATION" ]]; then builtin return; fi # this is defined outside _ksi_main to make it global without using declare -g # which is not available on older bash builtin declare -A _ksi_prompt -_ksi_prompt=( [cursor]='y' [title]='y' [mark]='y' [complete]='y' [ps0]='' [ps0_suffix]='' [ps1]='' [ps1_suffix]='' [ps2]='' ) +_ksi_prompt=( + [cursor]='y' [title]='y' [mark]='y' [complete]='y' [ps0]='' [ps0_suffix]='' [ps1]='' [ps1_suffix]='' [ps2]='' + [hostname_prefix]='' +) _ksi_main() { for i in ${KITTY_SHELL_INTEGRATION[@]}; do @@ -133,9 +136,10 @@ _ksi_main() { fi if [[ "${_ksi_prompt[title]}" == "y" ]]; then + [[ "$(builtin command who -m 2> /dev/null)" =~ "\([a-fA-F.:0-9]+\)$" ]] && _ksi_prompt[hostname_prefix]="\h: " # see https://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html#Controlling-the-Prompt # we use suffix here because some distros add title setting to their bashrc files by default - _ksi_prompt[ps1_suffix]+="\[\e]2;\w\a\]" + _ksi_prompt[ps1_suffix]+="\[\e]2;${_ksi_prompt[hostname_prefix]}\w\a\]" if [[ "$HISTCONTROL" == *"ignoreboth"* ]] || [[ "$HISTCONTROL" == *"ignorespace"* ]]; then _ksi_debug_print "ignoreboth or ignorespace present in bash HISTCONTROL setting, showing running command in window title will not be robust" fi @@ -144,7 +148,7 @@ _ksi_main() { last_cmd=$(HISTTIMEFORMAT= builtin history 1) last_cmd="${last_cmd#*[[:digit:]]*[[:space:]]}" # remove leading history number last_cmd="${last_cmd#"${last_cmd%%[![:space:]]*}"}" # remove remaining leading whitespace - builtin printf "\e]2;%s\a" "${last_cmd//[[:cntrl:]]}" # remove any control characters + builtin printf "\e]2;%s%s\a" "${_ksi_prompt[hostname_prefix]@P}" "${last_cmd//[[:cntrl:]]}" # remove any control characters } _ksi_prompt[ps0_suffix]+='$(_ksi_get_current_command)' fi diff --git a/shell-integration/zsh/kitty-integration b/shell-integration/zsh/kitty-integration index 7c786f194..9f73d6e52 100644 --- a/shell-integration/zsh/kitty-integration +++ b/shell-integration/zsh/kitty-integration @@ -64,7 +64,7 @@ typeset -gi _ksi_fd # Asks kitty to print $@ to its STDOUT. This is for debugging. _ksi_debug_print() { builtin local data - data=$(command base64 <<<"${(j: :)@}") || builtin return + data=$(builtin command base64 <<<"${(j: :)@}") || builtin return # Removing all spaces rather than just \n allows this code to # work on broken systems where base64 outputs \r\n. builtin print -nu "$_ksi_fd" '\eP@kitty-print|'"${data//[[:space:]]}"'\e\\' @@ -232,10 +232,19 @@ _ksi_deferred_init() { # We use (V) in preexec to convert control characters to something visible # (LF becomes \n, etc.). This isn't necessary in precmd because (%) does it # for us. - functions[_ksi_precmd]+=" - builtin print -rnu $_ksi_fd \$'\\e]2;'\"\${(%):-%(4~|…/%3~|%~)}\"\$'\\a'" - functions[_ksi_preexec]+=" - builtin print -rnu $_ksi_fd \$'\\e]2;'\"\${(V)1}\"\$'\\a'" + + if [[ "$(builtin command who -m 2> /dev/null)" =~ "\([a-fA-F.:0-9]+\)$" ]]; then + # show the hostname via %m for SSH sessions + functions[_ksi_precmd]+=" + builtin print -Prnu $_ksi_fd \$'\\e]2;'\"%m: \${(%):-%(4~|…/%3~|%~)}\"\$'\\a'" + functions[_ksi_preexec]+=" + builtin print -Prnu $_ksi_fd \$'\\e]2;'\"%m: \${(V)1}\"\$'\\a'" + else + functions[_ksi_precmd]+=" + builtin print -rnu $_ksi_fd \$'\\e]2;'\"\${(%):-%(4~|…/%3~|%~)}\"\$'\\a'" + functions[_ksi_preexec]+=" + builtin print -rnu $_ksi_fd \$'\\e]2;'\"\${(V)1}\"\$'\\a'" + fi fi # Enable cursor shape changes depending on the current keymap.