From 9d5bb3b2f280cbb6c5e04b6ef147fa6e1da5aca5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 31 Oct 2023 15:53:45 +0530 Subject: [PATCH] bash integration: Also make sudo a function There is less need in bash since its sudo completion is not as buggy, but it does fix the sudo --edit issue --- shell-integration/bash/kitty.bash | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/shell-integration/bash/kitty.bash b/shell-integration/bash/kitty.bash index 261ac556a..983275448 100644 --- a/shell-integration/bash/kitty.bash +++ b/shell-integration/bash/kitty.bash @@ -16,6 +16,26 @@ if [[ -n "$KITTY_BASH_INJECT" ]]; then [[ -f "$1" && -r "$1" ]] && builtin return 0; builtin return 1; } + if [[ -n "$ksi_val" && "$ksi_val" != *no-sudo* ]]; then + # this must be done before sourcing user bashrc otherwise aliasing of sudo does not work + sudo() { + # Ensure terminfo is available in sudo + builtin local is_sudoedit="n" + for arg; do + if [[ "$arg" == "-e" || $arg == "--edit" ]]; then + is_sudoedit="y" + builtin break; + fi + [[ "$arg" != -* && "$arg" != *=* ]] && builtin break # command found + done + if [[ -z "$TERMINFO" || "$is_sudoedit" == "y" ]]; then + builtin command sudo "$@"; + else + builtin command sudo TERMINFO="$TERMINFO" "$@"; + fi + } + fi + if [[ "$kitty_bash_inject" == *"posix"* ]]; then _ksi_sourceable "$KITTY_BASH_POSIX_ENV" && { builtin source "$KITTY_BASH_POSIX_ENV" @@ -212,10 +232,6 @@ _ksi_main() { builtin alias edit-in-kitty="kitten edit-in-kitty" - if [[ "${_ksi_prompt[sudo]}" == "y" ]]; then - # Ensure terminfo is available in sudo - [[ -n "$TERMINFO" ]] && builtin alias sudo="sudo TERMINFO=\"$TERMINFO\"" - fi if [[ "${_ksi_prompt[complete]}" == "y" ]]; then _ksi_completions() {