>_ZSH-SUDO
One keybind toggles sudo on the current command line. Sudo absent — prepend it. Sudo already there — strip it along with builtin / command / env and their args. Empty line — pull the last command from history and slap sudo on it.
Install
# Zinit
zinit light MenkeTechnologies/zsh-sudo
# Oh My Zsh
git clone https://github.com/MenkeTechnologies/zsh-sudo \
"${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-sudo"
# then: plugins+=(zsh-sudo)
# Manual
git clone https://github.com/MenkeTechnologies/zsh-sudo
source zsh-sudo/sudo.plugin.zsh
Key binding
Wire the widget to whatever combo you prefer:
bindkey '^N' sudo-command-line # Ctrl-N # or the classic Esc-Esc pattern: bindkey '\e\e' sudo-command-line
Configuration
Override the prepended command (e.g. doas) by setting both variables to the same string before the plugin loads:
export ZPWR_SUDO_REGEX='doas' export ZPWR_SUDO_CMD='doas'
ZPWR_SUDO_CMD is prepended; ZPWR_SUDO_REGEX is what the parser hunts for when stripping.
State table
apt update | → sudo apt update |
sudo apt update | → apt update |
| (empty line) | → sudo <last command from history> |
The regex engine handles quoted commands, builtin/command prefixes, env with flags, leading variable assignments, and stacked sudo options (-u root -E, etc).
Sibling plugins
Part of the MenkeTechnologies zsh plugin family — the MenkeTechnologiesMeta umbrella:
- zsh-better-npm-completion — cache-aware
npm installcompletion - zsh-cargo-completion — live
cargo add/cargo installcompletion - zsh-cpan-completion — live MetaCPAN
cpan/cpanmcompletion - zsh-dotnet-completion — dotnet CLI completion
- zsh-expand — spacebar expansion (11,683 tests)
- zsh-gem-completion — ruby
gem installremote completion - zsh-git-acp — 159 git aliases + add/commit/push keybindings
- zsh-git-repo-cache — index every git repo on disk
- zsh-learn — MySQL-backed learning collection / quiz
- zsh-more-completions — 33,175-file mega completion corpus
- zsh-nginx — nginx commands + service-wrapper aliases
- zsh-pip-description-completion — pip remote completion with descriptions
- zsh-sed-sub — in-place sed substitution on the command line
- zsh-xcode-completions — xcodebuild / xcrun / swift completion