From 35d385e5ca71e291a845607d8a907d5c89fedefe Mon Sep 17 00:00:00 2001 From: Konano Date: Tue, 25 Feb 2025 11:19:44 +0800 Subject: [PATCH] add hr: __zsh_history_remove --- files/zsh/20-function.zsh | 53 +++++++++++++++++++++++++++++++++++++++ files/zsh/50-env.zsh | 2 -- files/zsh/51-alias.zsh | 1 + 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/files/zsh/20-function.zsh b/files/zsh/20-function.zsh index ec4d97b..bed275d 100644 --- a/files/zsh/20-function.zsh +++ b/files/zsh/20-function.zsh @@ -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\"" +} diff --git a/files/zsh/50-env.zsh b/files/zsh/50-env.zsh index 3e58aab..7d172d0 100644 --- a/files/zsh/50-env.zsh +++ b/files/zsh/50-env.zsh @@ -28,5 +28,3 @@ if [ -f "/proc/version" ]; then export DISPLAY=:0; fi fi - -export HISTCONTROL=ignoreboth diff --git a/files/zsh/51-alias.zsh b/files/zsh/51-alias.zsh index a9d2191..c32d504 100644 --- a/files/zsh/51-alias.zsh +++ b/files/zsh/51-alias.zsh @@ -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'