Discover, install, and configure shell plugins with Fig Plugin Store →
☀️

Zsh Toggle Command Prefix

Zsh widget to toggle a prefix on a command

xPMo
|
16 stars
0 forks

Toggle Command Prefix

A Zsh widget to toggle a prefix on a command

By default, this will set "Alt+s" to toggle sudo on a command.

In its current state, it uses split-shell-arguments to divide up the buffer, and looks for newlines, ;/&&/||/|, followed optionally by if/else/while/etc.

This method should avoid any false positives, but it will also ignore $(command substitutions).

This project also makes use of all sudo flags known by the completion function _sudo and deletes them and their arguments if found as well. For example, sudo -uuser -g group --shell foo bar will be replaced by foo bar, since -u and -g take arguments, while --shell does not.

Default widgets:

Widget Binding Prefix Inserted Prefix Matched
toggle-sudo Alt-s sudo sudo followed by any number of supported flags known by _sudo completion
toggle-noglob Alt-g noglob noglob
toggle-nocorrect (not bound) nocorrect nocorrect
toggle-command Alt-Shift-c command command

Examples:

Launch a command with i3-msg instead of in the current session.

.toggle-i3exec(){
    .toggle-command-prefix 'i3-msg -- exec -- ' 'i3-msg' '(--|exec)'
}
zle -N .toggle-i3exec
bindkey '^[i' .toggle-i3exec

Launch a command with nice

.toggle-nice(){
    .toggle-command-prefix 'nice -n10 ' 'nice' '(-n?|--adjustment=)*' '(-n|--adjustment)'
}
zle -N .toggle-nice
bindkey '^[n' .toggle-nice

Configuration

Toggle-command-prefix uses zstyle settings to control certain behavior.

CUTBUFFER

If a command is found and removed, the following lookup is done:

zstyle -t :zle:toggle-command-prefix:${removed_text} copy

If the lookup returns true, then the remove text is also yanked into the cut buffer for later pasting.

You can use this like so:

# copy by default
zstyle ':zle:toggle-command-prefix:*' copy true
# don't copy sudo by itself
zstyle ':zle:toggle-command-prefix:sudo ' copy false
# copy sudo followed by any number of other matched flags
zstyle ':zle:toggle-command-prefix:sudo -*' copy true