the history -d
arg takes a range and $HISTCMD
is the max number in the history.
This function works to remove the last n
entries from history (just pass in the number of history commands to remove like, eg rmhist 5
) :
$ rmhist() { history -d $(($HISTCMD - $1))-$HISTCMD ;}
Or.. Go fancy with an arg like this to remove from a point in history (inclusive) or last 'n' commands:
rmhist() { case $1 in --from|from) local start=$2; ;; --last|last) local start=$(($HISTCMD - $2)) ;; *) echo "Try rmhist --from # or rmhist --last n "; return ;; esac; history -d ${start}-${HISTCMD}}
The result looks something like this:
5778 ls /etc 5779 ls /tmp 5780 ls /dev 5781 ll 5782 cd /tmp 5783 cd 5784 history(base) ~ $ rmhist --last 3(base) ~ $ history 5 5778 ls /etc 5779 ls /tmp 5780 ls /dev 5781 ll 5782 history 5(base) ~ $ rmhist --from 5780(base) ~ $ history 5 5776 history 10 5777 ls 5778 ls /etc 5779 ls /tmp 5780 history 5