cd17aed9e109692273b5c251414ee733cba704f2

Author
daddeffe <daddef@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

sudo: keep space before the command to ignore it in the history (#9178)

Co-authored-by: Marc Cornellà <marc.cornella@live.com>

Diff

 1diff --git a/plugins/sudo/sudo.plugin.zsh b/plugins/sudo/sudo.plugin.zsh
 2index 51579377d07fc78ea371c47e27adc7d7aaaa2790..b3749eff79f0663837c6b87a367399e21436d30a 100644
 3--- a/plugins/sudo/sudo.plugin.zsh
 4+++ b/plugins/sudo/sudo.plugin.zsh
 5@@ -14,6 +14,14 @@
 6 
 7 sudo-command-line() {
 8     [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
 9+
10+    # Save beginning space
11+    local WHITESPACE=""
12+    if [[ ${LBUFFER:0:1} == " " ]] ; then 
13+        WHITESPACE=" "
14+        LBUFFER="${LBUFFER:1}"
15+    fi
16+
17     if [[ -n $EDITOR && $BUFFER == $EDITOR\ * ]]; then
18         if [[ ${#LBUFFER} -le ${#EDITOR} ]]; then
19             RBUFFER=" ${BUFFER#$EDITOR }"
20@@ -38,6 +46,9 @@ sudo-command-line() {
21     else
22         LBUFFER="sudo $LBUFFER"
23     fi
24+
25+    # Preserve beginning space
26+    LBUFFER="${WHITESPACE}${LBUFFER}"
27 }
28 zle -N sudo-command-line
29 # Defined shortcut keys: [Esc] [Esc]