Following https://www.baeldung.com/linux/history-remove-avoid-duplicates, I added
clean_history() { awk '!a[$0]++' $HOME/.bash_history > $HOME/.bash_history.tmp && mv $HOME/.bash_history.tmp $HOME/.bash_history } trap clean_history DEBUG # execute after every command to my ~/.bashrc file in order to ensure that after every command is executed, duplicate entries are removed. This is important for me, because kile (the KDE LaTeX editor) leaves a ton of "clear" and "cd ..." commands in the history, effectively destroying the history within a single session of LaTeX-writing. And I have not found any way to prevent kile to do that.
Anyway... the above line of code effectively removes all subsequent entries of a command and leaves the first entry in the history. However, I want the last entry to remain in the history and all previous entries to be deleted. How can do this?
By the way: Kile ignores
HISTCONTROL=ignoredups:erasedups so using this approach seems not to be an option.
I would also be happy if we could prevent Kile to write into the history all together.
Non-duplicates:
- Removing duplicate entries in text file is leaving the first entry in the file
- so does How to remove duplicate lines inside a text file & get the number of the removed lines?
- How to stop Bash appending history uses
HISTCONTROL
.bashrcand just disable history recording.cd ..and I useCtrl-Lfor aclear, so having these in history ever seems unnecessary. I useHISTIGNORE=for things I see aspollutants, so your~/.bashrccould have e.g.HISTIGNORE="cd:cd ..:pwd:exit:mc:clear:history *"...