Click, copy "hist.sh" above, then paste, prepend the Bash functions inside to ~/.bashrc
file
Tidying up Bash commands history by having good control in removing certain line(s) specified by number(s) or range or by a string segment lying in a history line.
Bash 5
sed
head
bash code export HISTCONTROL=erasedups:ignoredups
simply type h
then it'll show the latest 25 lines of shell commands e.g:
$ h
367 pushd --help
368 pushd -n debian/
369 pushd build/
370 debuild -i -uc -us -b
371 ls debuild*
372 ls -hs
373 pushd ~
374 rm .config/autostart-scripts/autostart.sh
375 popd
377 cd
378 cd debian/
379 cat -n install
Up/Down. n[=-n] by line or else string:Up/Down. n[=-n] by line or else string:
367 371-373
or 367 373-371
to remove lines 367, 371, 372, 373 (reverse range boundary numbers doesn't matter). Note the requirement: A one line multiple numbers/ranges values must be in ascending order for any number range format (such above and e.g. --3 is less than --7 etc) otherwise it'd delete erronously, however the output is always in its reverse i.e. descending order. If the range upper number is the last one it can be omitted so 371-
it will remove lines 371, 372, ...379 371=2
. It means 371 and 2 lines succeding it i.e. 371-373 while 371=-3
means 371 and the 3 lines preceding it i.e. 368-371 [-]-number[-number]
, it will delete the number ordered from the end (reverse order line number), or if the dash is two, the last number lines, all are relative to lines list being shown then. For example: -5
, will remove the 5th line ordered from the last of lines being shown. A single dash alone -
is short for -1
to remove the last line =
and a number: -5=3
, is to remove the 5th line ordered from the last including also the next 3 lines, and -5=-3
is to remove the 5th line ordered from the last, also the 3 lines preceding it. Omitting the number -5=
means the number is 1, so equivalent to -5=1
--5
, will remove the last 5 lines relative to the last lines being shown. --5-2
remove the last 5 lines but the latest 2 lines, --5-
will remove the last 5 lines but the last line ...
(three period in a row) it becomes just OS shell *
wildcard character .
(single period) it becomes just OS shell ?
wildcard character while literal periode is input with \.
readline
) by preceding it with space firstIf one already knew the number or the searched string, then just put directly in shell/terminal prompt to delete it such as:
$ h 371-375 367
$ cd /home
It'b be also as history alias i.e.
$ h --help
shows history command's helpful reference
$ h -r
append to current history from file ~/.bash_history
Addition to history options, there is:
$ h -cr
reload the history from file ~/.bash_history
(clean up the current history then do the append previous above)
Except a mere h
cannot be as mere history
command itself to list entire numbered commands, as typing h
will get into this interactive history tidying so it needs a period following it:
$ h .
If quit by saving a modified history, it automatically cleans up every empty or space only content line
Just do h -w to ensure it saved in ~/.bash_history
after tidying up before exit the terminal