Preserve cursor position when prepending sudo.

This commit is contained in:
Michael Fladischer 2013-04-25 13:14:31 +02:00
commit 714091279f

View file

@ -2,20 +2,29 @@
# Description # 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 # Authors
# ------- # -------
# #
# * Dongweiming <ciici123@gmail.com> # * Dongweiming <ciici123@gmail.com>
# * Michael Fladischer <FladischerMichael@fladi.at>
# #
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
sudo_preserve_cursor=1
sudo-command-line() { sudo-command-line() {
[[ -z $BUFFER ]] && zle up-history [[ -z $BUFFER ]] && zle up-history
[[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER" if [[ $sudo_preserve_cursor -eq 1 ]]; then
zle end-of-line [[ $LBUFFER != sudo\ * ]] && LBUFFER="sudo $LBUFFER"
else
[[ $BUFFER != sudo\ * ]] && BUFFER="sudo $BUFFER"
zle end-of-line
fi
} }
zle -N sudo-command-line zle -N sudo-command-line
# Defined shortcut keys: [Esc] [Esc] # Defined shortcut keys: [Esc] [Esc]