From b220c0e7441ab4e55c514b9d2348c103de0c4cd7 Mon Sep 17 00:00:00 2001 From: Uco Mesdag Date: Mon, 13 Apr 2020 02:30:28 +0200 Subject: [PATCH 1/3] Plugin to toggle sudo before your current or last command --- plugins/available/sudo.plugin.bash | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 plugins/available/sudo.plugin.bash diff --git a/plugins/available/sudo.plugin.bash b/plugins/available/sudo.plugin.bash new file mode 100644 index 00000000..f18219b5 --- /dev/null +++ b/plugins/available/sudo.plugin.bash @@ -0,0 +1,15 @@ +cite about-plugin +about-plugin 'Toggle sudo before your current or last command' + +function sudo-command-line() { + [[ ${#READLINE_LINE} -eq 0 ]] && READLINE_LINE=$(fc -l -n -1 | xargs) + if [[ $READLINE_LINE == sudo\ * ]]; then + READLINE_LINE="${READLINE_LINE#sudo }" + else + READLINE_LINE="sudo $READLINE_LINE" + fi + READLINE_POINT=${#READLINE_LINE} +} + +# Define shortcut keys: [Esc] [Esc] +bind -x '"\e\e": sudo-command-line' From e73f6dbc620dfae27d186a98caa8f066df06ebe8 Mon Sep 17 00:00:00 2001 From: Uco Mesdag Date: Mon, 13 Apr 2020 02:37:24 +0200 Subject: [PATCH 2/3] Reworded the about --- plugins/available/sudo.plugin.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/available/sudo.plugin.bash b/plugins/available/sudo.plugin.bash index f18219b5..26c53853 100644 --- a/plugins/available/sudo.plugin.bash +++ b/plugins/available/sudo.plugin.bash @@ -1,5 +1,5 @@ cite about-plugin -about-plugin 'Toggle sudo before your current or last command' +about-plugin 'Toggle sudo at the beginning of the current or the previous command' function sudo-command-line() { [[ ${#READLINE_LINE} -eq 0 ]] && READLINE_LINE=$(fc -l -n -1 | xargs) From dfeb72d6aa5f3b72f5d8ccecaf2748b17d220e4e Mon Sep 17 00:00:00 2001 From: Uco Mesdag Date: Mon, 13 Apr 2020 13:35:52 +0200 Subject: [PATCH 3/3] Added function metadata and bash version check --- plugins/available/sudo.plugin.bash | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plugins/available/sudo.plugin.bash b/plugins/available/sudo.plugin.bash index 26c53853..eb155daa 100644 --- a/plugins/available/sudo.plugin.bash +++ b/plugins/available/sudo.plugin.bash @@ -1,7 +1,10 @@ cite about-plugin -about-plugin 'Toggle sudo at the beginning of the current or the previous command' +about-plugin 'Toggle sudo at the beginning of the current or the previous command by hitting the ESC key twice' function sudo-command-line() { + about "toggle sudo at the beginning of the current or the previous command by hitting the ESC key twice" + group "sudo" + [[ ${#READLINE_LINE} -eq 0 ]] && READLINE_LINE=$(fc -l -n -1 | xargs) if [[ $READLINE_LINE == sudo\ * ]]; then READLINE_LINE="${READLINE_LINE#sudo }" @@ -12,4 +15,8 @@ function sudo-command-line() { } # Define shortcut keys: [Esc] [Esc] -bind -x '"\e\e": sudo-command-line' + +# Readline library requires bash version 4 or later +if [ "${BASH_VERSINFO}" -ge 4 ]; then + bind -x '"\e\e": sudo-command-line' +fi