Created
June 8, 2025 17:08
-
-
Save Zetaphor/7806f15319589ada997e3db5afe3f2c9 to your computer and use it in GitHub Desktop.
Fish history deduplication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dedup_fish_history --description "Deduplicate Fish shell history (compatible with old/new versions)" | |
# 1. Get the path to the history file in a backwards-compatible way. | |
set -l histfile | |
# Try the modern way first. | |
if command -q fish_config && fish_config path history >/dev/null 2>&1 | |
set histfile (fish_config path history) | |
else | |
# Fall back to the legacy default path for older fish versions. | |
set histfile "$HOME/.local/share/fish/fish_history" | |
# Some very old versions might use ~/.fish_history | |
if not test -f "$histfile" | |
set histfile "$HOME/.fish_history" | |
end | |
end | |
if not test -f "$histfile" | |
echo (set_color red)"History file could not be found."(set_color normal) | |
return 1 | |
end | |
# 2. Create a timestamped backup for safety. | |
set -l backup_file "$histfile.bak."(date +%F-%T) | |
cp -p "$histfile" "$backup_file" | |
echo "↪ Safety backup created at:" (set_color blue)$backup_file(set_color normal) | |
# 3. Use a secure temporary file for the new history. | |
set -l temp_file (mktemp) | |
# 4. The core logic (this part remains the same). | |
echo "Deduplicating..." | |
tac "$histfile" | awk ' | |
/^-\scmd:\s/ { | |
cmd = substr($0, 8) | |
if (!seen[cmd]++) { | |
if (prev_line ~ /^\s+when:\s/) { | |
print prev_line | |
} | |
print $0 | |
} | |
} | |
{ prev_line = $0 } | |
' | tac > "$temp_file" | |
# 5. Calculate stats. | |
set -l old_count (math (string trim --right (wc -l < "$backup_file")) / 2) | |
set -l new_count (math (string trim --right (wc -l < "$temp_file")) / 2) | |
# 6. Replace the original history file. | |
mv "$temp_file" "$histfile" | |
# 7. Reload the history for the current session. | |
history clear-session | |
history merge | |
echo (set_color green)"✅ History deduplicated successfully."(set_color normal) | |
echo " Commands before: " (string trim $old_count) | |
echo " Commands after: " (string trim $new_count) | |
echo " Commands removed:" (string trim (math $old_count - $new_count)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment