This commit is contained in:
2026-03-28 07:53:51 +01:00
parent 9f57351949
commit 02691d4e42
10 changed files with 529 additions and 23 deletions
+45 -20
View File
@@ -1,29 +1,54 @@
# Remove the last command from history (both memory and file).
# The alias has a leading space so "oops" itself is not recorded (HIST_IGNORE_SPACE).
uy_oops_confirm() {
if [[ ! -f "$HISTFILE" ]]; then
print -P "%F{red}Failed to locate history file: $HISTFILE%f"
return 1
fi
# Flush current session to file so we operate on the latest state
fc -W
local last_line=$(tail -n 1 "$HISTFILE")
if [[ ! -f "$HISTFILE" || ! -s "$HISTFILE" ]]; then
print -P "%F{yellow}History is empty, nothing to delete.%f"
return 1
fi
if [[ -z "$last_line" ]]; then
print -P "%F{yellow}History file is empty, nothing to delete.%f"
return 0
fi
# Read the last entry (may span multiple lines if it ends with \)
local -a entry
local line
while IFS= read -r line; do
entry+=("$line")
# EXTENDED_HISTORY continuation lines end with a literal backslash
[[ "$line" == *'\' ]] || break
done < <(tail -n 50 "$HISTFILE" | tac)
# entry is reversed (last line first), flip it back
local -a ordered
for (( i=${#entry[@]}; i>=1; i-- )); do
ordered+=("${entry[$i]}")
done
print -P "About to permanently delete the last command from history file:"
print -P "%F{red} $last_line%f"
if (( ${#ordered[@]} == 0 )); then
print -P "%F{yellow}Could not parse last history entry.%f"
return 1
fi
local reply
echo -n "Proceed? [Y/n] "
read -r reply
print -P "About to permanently delete the last command from history:"
for line in "${ordered[@]}"; do
print -P " %F{red}${line}%f"
done
if [[ -z "$reply" || "$reply" == [yY]* ]]; then
sed -i '$d' "$HISTFILE"
print -P "%F{green}Command removed from history file (may still visible in current session).%f"
else
print -P "%F{yellow}Operation cancelled.%f"
fi
local reply
echo -n "Proceed? [Y/n] "
read -r reply
if [[ -z "$reply" || "$reply" == [yY]* ]]; then
# Delete the last N lines (the full entry) from the file
local n=${#ordered[@]}
head -n -"$n" "$HISTFILE" > "$HISTFILE.tmp" && mv "$HISTFILE.tmp" "$HISTFILE"
# Reload history from the updated file
fc -R
print -P "%F{green}Deleted ($n line(s) removed).%f"
else
# Reload anyway to stay in sync
fc -R
print -P "%F{yellow}Cancelled.%f"
fi
}
alias oops=' uy_oops_confirm'