Browse Source

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

Co-authored-by: Marc Cornellà <marc.cornella@live.com>
daddeffe 4 years ago
parent
commit
cd17aed9e1
1 changed files with 11 additions and 0 deletions
  1. 11 0
      plugins/sudo/sudo.plugin.zsh

+ 11 - 0
plugins/sudo/sudo.plugin.zsh

@@ -14,6 +14,14 @@
 
 
 sudo-command-line() {
 sudo-command-line() {
     [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
     [[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
+
+    # Save beginning space
+    local WHITESPACE=""
+    if [[ ${LBUFFER:0:1} == " " ]] ; then 
+        WHITESPACE=" "
+        LBUFFER="${LBUFFER:1}"
+    fi
+
     if [[ -n $EDITOR && $BUFFER == $EDITOR\ * ]]; then
     if [[ -n $EDITOR && $BUFFER == $EDITOR\ * ]]; then
         if [[ ${#LBUFFER} -le ${#EDITOR} ]]; then
         if [[ ${#LBUFFER} -le ${#EDITOR} ]]; then
             RBUFFER=" ${BUFFER#$EDITOR }"
             RBUFFER=" ${BUFFER#$EDITOR }"
@@ -38,6 +46,9 @@ sudo-command-line() {
     else
     else
         LBUFFER="sudo $LBUFFER"
         LBUFFER="sudo $LBUFFER"
     fi
     fi
+
+    # Preserve beginning space
+    LBUFFER="${WHITESPACE}${LBUFFER}"
 }
 }
 zle -N sudo-command-line
 zle -N sudo-command-line
 # Defined shortcut keys: [Esc] [Esc]
 # Defined shortcut keys: [Esc] [Esc]