Skip to content

Instantly share code, notes, and snippets.

@ssghost
Forked from maurelian/gist:859d8bbce79ef8facdd9eb8493f9b029
Last active June 11, 2023 03:44
Show Gist options
  • Save ssghost/2d24dd2112f40f04119a374811a4a4e4 to your computer and use it in GitHub Desktop.
Save ssghost/2d24dd2112f40f04119a374811a4a4e4 to your computer and use it in GitHub Desktop.
A zsh function to delete n lines of .zsh_history
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