summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChloƩ Vulquin <code@toast.bunkerlabs.net>2026-01-24 12:13:30 +0100
committerChloƩ Vulquin <code@toast.bunkerlabs.net>2026-01-24 12:13:30 +0100
commitdd6e1304420396e33f3a7f99918ec9d0e777f1b1 (patch)
tree20fe93444d87d1ec6ea095ff9995d0851e4503c2
parenteditorconfig: default to indentation of 4, regardless of tabs/spaces (diff)
zsh: add elevate-cmd and edit-command-line bindings
-rw-r--r--dot_config/zsh/functions/elevate-cmd36
-rw-r--r--dot_config/zsh/zshrc6
2 files changed, 42 insertions, 0 deletions
diff --git a/dot_config/zsh/functions/elevate-cmd b/dot_config/zsh/functions/elevate-cmd
new file mode 100644
index 0000000..7afe23b
--- /dev/null
+++ b/dot_config/zsh/functions/elevate-cmd
@@ -0,0 +1,36 @@
+# -*- mode: sh *-*
+# vim: ft=zsh
+
+# the de-facto prefix is determined by:
+# * zstyle (by widget name) if it's set
+# * else the dedicated environment variable
+# * else it's detected at runtime
+local prefix
+if ! zstyle -s :zle:$WIDGET prefix prefix; then
+ prefix=$ELEVATECMD
+ if [[ -z $prefix ]]; then
+ # the runtime detection is uncached in case you install/uninstall
+ if [[ -x =doas ]]; then prefix=doas
+ elif [[ -x =sudo ]]; then prefix=sudo
+ else return 1 # do nothing
+ fi
+ fi
+fi
+
+# if the prefix is already present, we remove it
+if [[ $BUFFER = ${(b)~prefix}\ * ]]; then
+ # if the cursor is not inside the prefix
+ if [[ $#LBUFFER -gt $#prefix ]]; then
+ # then just remove it from the LBUFFER
+ LBUFFER=${LBUFFER#$prefix }
+ # otherwise, the cursor *is* inside the prefix
+ else
+ # so remove it from the buffer and move the cursor to start of line
+ BUFFER=${BUFFER#$prefix }
+ CURSOR=0
+ fi
+# otherwise we add it
+else
+ # prefix always goes into LBUFFER since it's not there yet
+ LBUFFER="$prefix $LBUFFER"
+fi
diff --git a/dot_config/zsh/zshrc b/dot_config/zsh/zshrc
index cab9db9..0b56623 100644
--- a/dot_config/zsh/zshrc
+++ b/dot_config/zsh/zshrc
@@ -6,7 +6,13 @@ xzsource zshrc.pre
xzsource zshrc.{options,style}
### Bindings
+autoload -Uz edit-command-line elevate-cmd
+zle -N edit-command-line
+zle -N elevate-cmd
+
bindkey -e
+bindkey '^x^e' edit-command-line
+bindkey '^[s' elevate-cmd
### Functions, Aliases
## Command replacements