bash 的 !$ 和 !! 等价物是什么?在鱼壳里?

Fra*_*itt 35 bash shell command-line fish

我已经使用 bash 十多年了,并且已经习惯于能够输入 !$ 来重复最后一个参数,例如:

$ mkdir foo
$ cd !$
cd foo
Run Code Online (Sandbox Code Playgroud)

(最后一行是由 shell 打印出来的,告诉你你的命令的计算结果)。同样,我经常对以下内容:

$ make_sandwich
-bash: make_sandwich: Permission denied
$ sudo !!
sudo make_sandwich
Run Code Online (Sandbox Code Playgroud)

我真的很喜欢鱼壳,但我的肌肉记忆已经很成熟了。鱼有等价物吗?我可以配置 fish 以使用相同的命令吗?

gle*_*man 31

Alt-Up arrow为您提供上一个命令的最后一个参数。随后的推送循环遍历先前的参数。

我还没有找到一个满意相当于!!,除了Up那么Ctrl-A

  • `alt`-`.` 对我有用。 (2认同)

gle*_*man 15

我在fish-users 邮件列表上找到了这个完美的答案:

function bind_bang
    switch (commandline -t)[-1]
        case "!"
            commandline -t $history[1]; commandline -f repaint
        case "*"
            commandline -i !
    end
end

function bind_dollar
    switch (commandline -t)[-1]
        case "!"
            commandline -t ""
            commandline -f history-token-search-backward
        case "*"
            commandline -i '$'
    end
end

function fish_user_key_bindings
    bind ! bind_bang
    bind '$' bind_dollar
end
Run Code Online (Sandbox Code Playgroud)

关于fish的github wiki的进一步讨论

  • 我创建了`~/.config/fish/config.fish` 并粘贴了它。重新启动鱼后效果很好。 (2认同)

小智 13

为了sudo !!

\n

在fish shell(版本3.1b1及更高版本)中,您可以使用绑定__fish_prepend_sudo(默认情况下为Alt+ S)在当前存在的命令行前面加上sudo

\n
$ make sandwich\xe2\x96\x8e \n
Run Code Online (Sandbox Code Playgroud)\n

Alt+S

\n
$ sudo make sandwich\xe2\x96\x8e\n
Run Code Online (Sandbox Code Playgroud)\n

从 Fish 3.2.0 开始,空行上的Alt+S将从历史记录中拉出最新的行并添加到前面sudo 。在 3.2.0 之前,您首先需要通过 . 从历史记录中提取最新的行

\n

从 Fish 3.5.0 开始,doas如果系统上可用但sudo不可用,则将使用该命令。

\n
\n

对于除sudo/之外的前置命令doas

\n

请参阅@GlennJackman\'s 答案bind_bang中的函数

\n
\n

为了<command> !$

\n

您可以使用绑定循环遍历先前命令的参数history-token-search-backward。默认的键绑定是Alt+ .,如 Bash 中一样,但也可以使用Alt+Alt+ :

\n
$ mkdir foo\n$ rmdir \xe2\x96\x8e\n
Run Code Online (Sandbox Code Playgroud)\n

Alt+.

\n
$ rmdir foo\xe2\x96\x8e\n
Run Code Online (Sandbox Code Playgroud)\n

fish_config可以通过执行并导航到“绑定”选项卡来查看鱼的所有绑定。

\n


Car*_*rot 11

须藤!!(或 sudo bang bang)是我最常用的命令之一。我仍然只使用普通的旧 bash,它已经很好了。很遗憾听到鱼没有正确实现它。有点谷歌搜索,我发现了这个:

function sudo
    if test "$argv" = !!
        eval command sudo $history[1]
    else
        command sudo $argv
    end
end
Run Code Online (Sandbox Code Playgroud)

这里的线程有更多选项:https : //github.com/fish-shell/fish-shell/issues/288


Sid*_*med 5

我和你有同样的问题,我通过使用oh-my-fish (它是fishshell的插件管理器)https://github.com/oh-my-fish/oh-my-fish来修复 。您可以使用以下命令安装它:

curl -L https://get.oh-my.fish | fish
Run Code Online (Sandbox Code Playgroud)

然后bang-bang使用以下命令安装插件:

omf install bang-bang 
Run Code Online (Sandbox Code Playgroud)