有选择地将命令历史记录存储到单独的文件中

Selectively storing command history to a separate file

我想修改 fish shell 中的历史记录机制,以便所有以 'apt' 开头的命令都应该(也)存储在一个单独的文件中。

有没有不用重新编译 fish 的方法?如果能把这个功能打开就更好了on/off.

谢谢!

您最有可能想做的是查看 /var/log/apt/,apt 存储其日志的位置。这些比您可以在 shell 中组合在一起的任何内容都更具权威性,因为 apt 实际上是在编写它们。


现在,如果您必须在 fish 中执行此操作,您可以挂钩 fish 的事件,例如 fish_preexec 事件,即 运行 执行命令之前的事件:

function saveapt --on-event fish_preexec
    # The event hands the commandline over as the first argument
    # This really just checks if the commandline starts with 'apt',
    # so it won't e.g. detect `command apt` or calling it via functions
    # or scripts.
    if string match -r '^apt' -- $argv
        echo (date) $argv >> ~/apt_history.txt
    end
end

保存在急切来源的文件中,例如 config.fish 或 conf.d/*.fish