From 714091279fa1f31c4985083db39794bedda9269f Mon Sep 17 00:00:00 2001 From: Michael Fladischer Date: Thu, 25 Apr 2013 13:14:31 +0200 Subject: [PATCH] Preserve cursor position when prepending sudo. --- plugins/sudo/sudo.plugin.zsh | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/plugins/sudo/sudo.plugin.zsh b/plugins/sudo/sudo.plugin.zsh index d12e06853..dc6bcfbc8 100644 --- a/plugins/sudo/sudo.plugin.zsh +++ b/plugins/sudo/sudo.plugin.zsh @@ -2,20 +2,29 @@ # Description # ----------- # -# sudo will be inserted before the command +# sudo will be inserted before the command and the cursor position will be +# preserved. If the prompt is empty, the previously executed command will be +# fetched from the history and prepended with "sudo ". # # ------------------------------------------------------------------------------ # Authors # ------- # # * Dongweiming +# * Michael Fladischer # # ------------------------------------------------------------------------------ +sudo_preserve_cursor=1 + sudo-command-line() { -[[ -z $BUFFER ]] && zle up-history -[[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER" -zle end-of-line + [[ -z $BUFFER ]] && zle up-history + if [[ $sudo_preserve_cursor -eq 1 ]]; then + [[ $LBUFFER != sudo\ * ]] && LBUFFER="sudo $LBUFFER" + else + [[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER" + zle end-of-line + fi } zle -N sudo-command-line # Defined shortcut keys: [Esc] [Esc]