-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-script-prefix.plugin.zsh
49 lines (44 loc) · 1.49 KB
/
zsh-script-prefix.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# ------------------------------------------------------------------------------
# Description
# -----------
#
# zsh-script-prefix ./ (replacement for script-prefix) will be inserted before the script
# Based on sudo plugin
#
# ------------------------------------------------------------------------------
# Authors
# -------
# * tmiland <kontakt@tmiland.com>
# - Original sudo script:
# * Dongweiming <ciici123@gmail.com>
# * Subhaditya Nath <github.com/subnut>
# * Marc Cornellà <github.com/mcornella>
# * Carlo Sala <carlosalag@protonmail.com>
#
# ------------------------------------------------------------------------------
__script-prefix-replace-buffer() {
local old=$1 new=$2
# if the cursor is positioned in the $old part of the text, make
# the substitution and leave the cursor after the $new text
if [[ $CURSOR -le ${#old} ]]; then
BUFFER="${new}${BUFFER#$old }"
CURSOR=${#new}
# otherwise just replace $old with $new in the text before the cursor
else
LBUFFER="${new}${LBUFFER#$old }"
fi
}
script-prefix-command-line() {
# If line is empty, get the last run command from history
[[ -z $BUFFER ]] && LBUFFER="$(fc -ln -1)"
case "$BUFFER" in
\./\ *) __script-prefix-replace-buffer "./" "" ;;
*) LBUFFER="./$LBUFFER" ;;
esac
return
}
zle -N script-prefix-command-line
# Defined shortcut keys: [Esc]
bindkey -M emacs '\e' script-prefix-command-line
bindkey -M vicmd '\e' script-prefix-command-line
bindkey -M viins '\e' script-prefix-command-line