// ZSH-SUDO — TOGGLE sudo ON THE CURRENT COMMAND LINE

zsh plugin · single ZLE widget · prepend sudo if missing · strip sudo (and builtin / command / env wrappers) if present · recall last command from history if empty

GitHub Issues
// Color scheme

>_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 updatesudo apt update
sudo apt updateapt 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: