add hr: __zsh_history_remove

This commit is contained in:
Konano 2025-02-25 11:19:44 +08:00
parent 246572fd75
commit 35d385e5ca
Signed by: Nano
GPG Key ID: 93686B78EE43A65A
3 changed files with 54 additions and 2 deletions

View File

@ -50,3 +50,56 @@ __zsh_get_ip() {
__zsh_get_ipv4
__zsh_get_ipv6
}
__zsh_history_remove() {
if [ -z "$1" ]; then
echo "Please specify a keyword to remove"
return 1
fi
# Check if HISTFILE exists
if [ -z "$HISTFILE" ]; then
echo "Error: HISTFILE is not set"
return 1
fi
local keyword="$1"
local backup_file="${HISTFILE}.backup"
# Check if there are any matching entries
if ! grep -q "$keyword" "$HISTFILE"; then
echo "No matching entries found for: $keyword"
return 0
fi
# Show matching entries
echo "The following history entries will be removed:"
echo "----------------------------------------"
grep --color=always "$keyword" "$HISTFILE"
echo "----------------------------------------"
# Confirmation
echo -n "Are you sure you want to remove these entries? (y/N) "
read confirm
if [[ ! "$confirm" =~ ^[yY](es)?$ ]]; then
echo "Operation cancelled"
return 0
fi
# Create backup
cp "$HISTFILE" "$backup_file"
echo "Backup created at: $backup_file"
# Remove entries containing keyword
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
sed -i '' "/${keyword}/d" "$HISTFILE"
else
# Linux
sed -i "/${keyword}/d" "$HISTFILE"
fi
echo "Removal completed"
echo "If needed, you can restore from backup using:"
echo "cp \"$backup_file\" \"$HISTFILE\""
}

View File

@ -28,5 +28,3 @@ if [ -f "/proc/version" ]; then
export DISPLAY=:0;
fi
fi
export HISTCONTROL=ignoreboth

View File

@ -46,6 +46,7 @@ command_exists proxychains4 && alias ps4='proxychains4 -f ~/.lazy/files/proxycha
# functions from 20-function.zsh
alias ii='__zsh_get_ip'
alias hr='__zsh_history_remove'
# for Lazy
alias lazy='~/.lazy/run.sh'