Last active
June 21, 2023 21:30
-
-
Save maurelian/859d8bbce79ef8facdd9eb8493f9b029 to your computer and use it in GitHub Desktop.
A zsh function to delete n lines of .zsh_history
This file contains 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
delhist() { | |
local num_lines="$1" | |
local file="$HOME/.zsh_history" | |
if [[ ! "$num_lines" =~ ^[0-9]+$ ]]; then | |
echo "Invalid number of lines: $num_lines" | |
return 1 | |
fi | |
local total_lines=$(wc -l < "$file") | |
local lines_to_keep=$((total_lines - num_lines)) | |
if [[ "$lines_to_keep" -lt 0 ]]; then | |
echo "The file has fewer lines than the specified number." | |
return 1 | |
fi | |
head -n "$lines_to_keep" "$file" >! "$file.tmp" | |
# copy over the contents so we keep the .tmp file as a back up just in case | |
cp -f "$file.tmp" "$file" | |
echo "Deleted $num_lines line(s) from '$file'." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment