如何以 sudo 重新运行 zsh 中的最后一个命令
How to rerun last command in zsh as sudo
我正在使用 zsh
和 oh-my-zsh
。我正在尝试一些非常简单的事情:
alias fu='sudo !!'
因此,如果我发出的命令需要忘记 sudo
,我可以快速重新执行。
是的,我用谷歌搜索了一下,我什至看到了几个 examples 但 NONE 对我有用。例如:
ls
bla bla
!!
zsh: command not found: !!
我也试过:
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!
另一个:
alias redo='sudo $(history -p !!)'
# didn't work
我在 Mac 和 Ubuntu 中尝试过,同样的问题。
只是一个想法(未测试),基于您在问题中提供的 link(但它解释了 bash 解决方案):而不是别名 redo,你创建了这个名字的函数。在函数内部,你可以做一个
local last_hist=( $(fc -l -1) )
将最近执行的命令存储到数组 last_hist
中,但以历史编号为前缀。您通过
删除历史编号
shift last_hist
并通过
执行你的sudo命令
sudo "${last_hist[@]}"
但是,只写
是不是要多做一些工作?
sudo !!
而不是你精心设计的函数
redo
?
我正在使用 zsh
和 oh-my-zsh
。我正在尝试一些非常简单的事情:
alias fu='sudo !!'
因此,如果我发出的命令需要忘记 sudo
,我可以快速重新执行。
是的,我用谷歌搜索了一下,我什至看到了几个 examples 但 NONE 对我有用。例如:
ls
bla bla
!!
zsh: command not found: !!
我也试过:
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!
另一个:
alias redo='sudo $(history -p !!)'
# didn't work
我在 Mac 和 Ubuntu 中尝试过,同样的问题。
只是一个想法(未测试),基于您在问题中提供的 link(但它解释了 bash 解决方案):而不是别名 redo,你创建了这个名字的函数。在函数内部,你可以做一个
local last_hist=( $(fc -l -1) )
将最近执行的命令存储到数组 last_hist
中,但以历史编号为前缀。您通过
shift last_hist
并通过
执行你的sudo命令sudo "${last_hist[@]}"
但是,只写
是不是要多做一些工作?sudo !!
而不是你精心设计的函数
redo
?