
在我的相關貼文很多年前,我找到了一個解決方案,如何註解掉 bash 歷史記錄中保存的「危險」命令,這樣我就不會意外執行它們。
在 中實施相同的最佳解決方案是什麼zsh
?
是否zsh
提供了一些我可以用於此目的的功能?我認為,zsh
beieng 更靈活,這應該更容易zsh
。
作為參考,這是我一直在使用的bash
(基於 Stéphane Chazelas 接受的答案):
fixhist() {
local cmd time histnum
cmd=$(HISTTIMEFORMAT='<%s>' history 1)
histnum=$((${cmd%%[<*]*}))
time=${cmd%%>*}
time=${time#*<}
cmd=${cmd#*>}
case $cmd in
(cp\ *|mv\ *|rm\ *|cat\ *\>*|pv\ *|dd\ *)
history -d "$histnum" # delete
history -a
[ -f "$HISTFILE" ] && printf '#%s\n' "$time" " $cmd" >> "$HISTFILE";;
(*)
history -a
esac
history -c
history -r
}
2022 年 9 月 5 日更新:
公認的解決方案有效,但有意想不到的副作用。它搞亂了insert-last-word
按鍵綁定。這裡簡短的說明:
我使用我的“危險”命令之一:
rm zz
它已被添加到歷史記錄中並帶有註釋(根據需要):
history
...
# rm zz
讓我們為歷史記錄新增另一個命令
echo foo
Alt現在,當我想使用+循環歷史記錄時.,我得到以下結果:
echo <Alt> + .
foo
history
# rm zz
我沒有被提供,而是zz
被提供了整個評論命令# rm zz
。
我怎樣才能解決這個問題 ?
答案1
當然,使用zshaddhistory
鉤子函數並停用常規歷史記錄處理。
function zshaddhistory() {
# defang naughty commands; the entire history entry is in $1
if [[ $1 =~ "cp\ *|mv\ *|rm\ *|cat\ *\>|pv\ *|dd\ *" ]]; then
1="# $1"
fi
# write to usual history location
print -sr -- ${1%%$'\n'}
# do not save the history line. if you have a chain of zshaddhistory
# hook functions, this may be more complicated to manage, depending
# on what those other hooks do (man zshall | less -p zshaddhistory)
return 1
}
在 zsh 5.0.8 上進行了測試
% exec zsh
% echo good
good
% echo bad; rm /etc
bad
rm: /etc: Operation not permitted
% history | tail -4
299 exec zsh
300 echo good
301 # echo bad; rm /etc
302 history | tail -4
%
extendedhistory
這似乎也適用於選項集。
答案2
以下功能基於 thrig 的功能並進行了修復histignorespace
:
function zshaddhistory() {
if [[ $1 =~ "^ " ]]; then
return 0
elif [[ $1 =~ "cp\ *|mv\ *|rm\ *|cat\ *\>|pv\ *|dd\ *" ]]; then
1="# $1"
fi
# write to usual history location
print -sr -- ${1%%$'\n'}
# do not save the history line. if you have a chain of zshaddhistory
# hook functions, this may be more complicated to manage, depending
# on what those other hooks do (man zshall | less -p zshaddhistory)
return 1
}