首页 > 解决方案 > 防止fish命令通过fish_preexec执行

问题描述

我想预处理所有发送给鱼的命令。我知道这可以通过 来完成fish_preexec,因为它将文字命令作为参数传递。但是,我也想防止在某些条件下完全执行命令。我不想退出鱼壳;我想保持当前的 shell 活着,只打印一条"command not executed"消息。

鱼支持这样的东西吗?

标签: shellcommandfish

解决方案


这很棘手,但您可以通过将 enter 键重新绑定到自定义函数来做到这一点,如下所示:

function execute_ifnotls
    if test (commandline) = 'ls'
        echo
        echo "No"
        commandline ""
        commandline -f repaint
    else
        commandline -f execute
    end
end
bind \r execute_ifnotls
bind \n execute_ifnotls

产生:

> ls
No
> whoami
david

推荐阅读