Prefix all commands in shell

You could use a function like the following:

use_prefix () {
    while read -ra c; do
        "$@" "${c[@]}"
    done
}

This will accept your prefix as arguments to the use_prefix command and then read from stdin for each command to be prefixed.

Note: You will have to use ctrl+c to exit the while read loop.


You could use xargs, but with due regard to caveats associated with its use

xargs -L1 git

then type in init, add -A etc, one per line


With the zsh shell, you could run:

zle-line-init() if [[ $CONTEXT = start ]] LBUFFER=$zle_prefix$LBUFFER
zle -N zle-line-init

For the contents of $zle_prefix to be inserted at the start of the buffer when you start entering a new command line.

So you can do:

zle_prefix='sudo docker '

For that sudo docker to be inserted at the start of subsequent commands.

You could define another widget that primes $zle_prefix with what's currently before your cursor:

prime-zle-prefix() zle_prefix=$LBUFFER
zle -N prime-zle-prefix
bindkey '\eP' prime-zle-prefix

And then you can type

$ sudo docker Alt+Shift+P ls

Then sudo docker will be inserted for all subsequent command. When you're done, blank the line (Ctrl+U) and enter Alt+Shift+P again to empty $zle_prefix.