Is it possible to ignore or remove duplicate entries from the terminal history? Right now, when I click the up-arrow to go through my previous commands, there are lots of duplicates, or when I use the history
command, it shows lots of duplicate entries. Does pantheon-terminal have the ability to remove/ignore those duplicate entries?
3 Answers
The command history is a feature of the shell, which is bash, rather than the terminal emulator Pantheon-Terminal.
The behavior around duplicates in the bash command history can be configured by setting several variables whose name start with HIST
. In particular, HISTCONTROL
is a list of options to tune the behavior. If the option ignoredups
is included, duplicates are not added to the history. More usefully in my opinion, if erasedups
is included, when a duplicate command is issued, the earlier duplicate is removed from the list. Thus ignoredups
retains the duplicate at its earliest position while erasedups
retains the duplicate at its latest position.
The place to set this variable is the bash interactive initialization file .bashrc
in your home directory. Open this file in a text editor. Add a line like
HISTCONTROL=erasedups
Multiple options are separated by colons, so for example if you want to ignore lines that begin with spaces you need
HISTCONTROL=erasedups:ignorespace
Save .bashrc
. The changes will take effect the next time you start bash, i.e. the next time you open a terminal or a new tab. You can also type this command at the bash command line, it will take effect for the shell session in which you type it.
Note that the default .bashrc
on elementary OS already includes a setting for HISTCONTROL
. So you'll need to either edit the existing HISTCONTROL=…
line, or add your own at the end of the file (if you add your setting at the beginning, it'll be overridden).
Answered in comments by OP:
A couple of restarts and the setting finally "took". I added
export HISTCONTROL=ignoreboth:erasedups
to the bottom of my~/.bashrc
file. Thanks for your help Gilles.
To uniqely record every new command is tricky. First you need to add to
~/.profile
or similar:
HISTCONTROL=erasedups
PROMPT_COMMAND='history -w'
Then you need to add to ~/.bash_logout
:
history -a
history -w
.bashrc
? Did you start a new terminal? Where did you add the setting — the default.bashrc
already contains a setting forHISTCONTROL
?export HISTCONTROL=ignoreboth:erasedups
to the bottom of my ~/.bashrc file. Thanks for your help Gilles.