在我的fish函数中,我正在评估构造的命令行
eval (commandline)
,具体来说 - 我正在寻找fzf中的一些文件名,然后分析命令行是否被预先添加vim
.如果是 - 而不是之后返回vim filename
按回车,我只是评价它,正如我之前所示.
问题是,如果我评估它,而不是手动输入,它不会进入历史 - 例如,我不能通过按下来看它作为上一个命令.
我set -x history (commandline) $history
在eval之后尝试过,但它显示了一个错误set: Tried to change the read-only variable “history”
有没有办法手动将自定义字符串(在我的情况下,命令行缓冲区)添加到历史记录中?谢谢.
history --merge
不按时间顺序合并历史#2312.因此,即使我们手动添加命令,~/.config/fish/fish_history
我们也无法简单地按下来查看它(尽管如果你按下足够的话,你最终会得到它).
为了解决这个问题,我们可以制作一个fish_history
然后调用的副本builtin history --clear
,它将清除fish的内部历史记录和历史文件.然后,我们从我们的副本中恢复历史文件,附加我们的命令并调用history --merge
以将历史文件与现在空的内部历史记录合并.
function evalh
eval $argv
# backup history file
cp ~/.config/fish/fish_history /tmp/fish_history.tmp
# clear internal history and history file (using builtin means we don't get a prompt)
builtin history --clear
# restore history file
cp /tmp/fish_history.tmp ~/.config/fish/fish_history
# append our command
echo "- cmd:" $argv >> ~/.config/fish/fish_history
echo " when:" (date "+%s") >> ~/.config/fish/fish_history
# merge history file with (empty) internal history
history --merge
end
Run Code Online (Sandbox Code Playgroud)