I'm using zsh
with oh-my-zsh
. I'm trying something very simple:
alias fu='sudo !!'
So, if I issued a command that needed a forgotten sudo
I could quickly re-do it.
Yes, I googled for that and I even saw several examples but NONE worked for me. For example:
ls
bla bla
!!
zsh: command not found: !!
I also tried:
ls
bla bla
fc -e : -1
bla bla
# that worked! But let's see with sudo...
ls
bla bla
sudo fc -e : -1
# nothing happens!
Another:
alias redo='sudo $(history -p !!)'
# didn't work
I've tried in Mac and Ubuntu, same issues.
Just an idea (not tested), based on the link you provided in your question (but which explains a bash solution): Instead of an alias redo, you create a function of this name. Inside the function, you can do a
local last_hist=( $(fc -l -1) )
which stores the most recently executed command into the array last_hist
, but prefixed by the history number. You remove the history number by
shift last_hist
and execute your sudo command by
sudo "${last_hist[@]}"
However, is it so much more work to just write
sudo !!
instead of your carefully crafted function
redo
?