From cd17aed9e109692273b5c251414ee733cba704f2 Mon Sep 17 00:00:00 2001 From: daddeffe Date: Sun, 23 Aug 2020 22:58:08 +0200 Subject: [PATCH] sudo: keep space before the command to ignore it in the history (#9178) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marc Cornellà --- plugins/sudo/sudo.plugin.zsh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/plugins/sudo/sudo.plugin.zsh b/plugins/sudo/sudo.plugin.zsh index 51579377d..b3749eff7 100644 --- a/plugins/sudo/sudo.plugin.zsh +++ b/plugins/sudo/sudo.plugin.zsh @@ -14,6 +14,14 @@ sudo-command-line() { [[ -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 [[ ${#LBUFFER} -le ${#EDITOR} ]]; then RBUFFER=" ${BUFFER#$EDITOR }" @@ -38,6 +46,9 @@ sudo-command-line() { else LBUFFER="sudo $LBUFFER" fi + + # Preserve beginning space + LBUFFER="${WHITESPACE}${LBUFFER}" } zle -N sudo-command-line # Defined shortcut keys: [Esc] [Esc]