sti*_*ijn 8 bash command-history
bash 历史记录中经常有一些我不再需要的项目和/或太长,以至于与 fzf 一起使用时几乎匹配输入的任何文本,使得历史记录的模糊匹配有点无用。所以我正在寻找一种方法,能够在使用 fzf 查看历史记录时删除这些行,但这太天真了(加上错过了重新加载,但这不是这里的问题):
export FZF_CTRL_R_OPTS="--bind 'ctrl-d:execute-silent(history -d {1})'"
Run Code Online (Sandbox Code Playgroud)
它不会删除任何历史记录,我假设这可能是因为 fzf 运行类似的命令,$SHELL -c "history -d 1000"但这是一个新的 shell(我的 $SHELL 是 /bin/bash),它没有历史记录(或者至少不是当前的历史记录)壳)。这个假设正确吗?以及如何解决这个问题?
一种方法(如下所示,作为要放入 .bashrc 中的别名)是直接编辑 .bash_history 文件,使用 fzf 选择命令,然后在编辑后重新加载它:
alias histfzf='history -w; cat .bash_history | fzf > /tmp/to_remove; grep -vxFf /tmp/to_remove .bash_history > .new_bash_history; mv .new_bash_history .bash_history; rm /tmp/to_remove; history -r'
Run Code Online (Sandbox Code Playgroud)
每一步说明:
# write active history in memory to the .bash_history file
history -w;
# select commands from history (using fzf), and store them in a file:
cat .bash_history | fzf > /tmp/to_remove
# Find commands that don't appear in the to_remove list (and store in .new_bash_history:
grep -vxFf /tmp/to_remove .bash_history > .new_bash_history
# overwrite the history with the edited version:
mv .new_bash_history .bash_history;
# clean up:
rm /tmp/to_remove;
# reload history:
history -r
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2936 次 |
| 最近记录: |