9

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?

5
  • 1
    That's a feature of bash, not of the terminal. It works the same with all terminals and all distributions. What is the full content of your .bashrc? Did you start a new terminal? Where did you add the setting — the default .bashrc already contains a setting for HISTCONTROL? Jul 10, 2015 at 7:44
  • 3
    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.
    – dagrha
    Jul 11, 2015 at 21:35
  • 2
    @dazzle please post as an answer.
    – user3
    Jul 14, 2015 at 9:11
  • 1
    How is this unclear - he wants to remove duplicates from the bash history. There's even an answer.
    – user3
    Jul 14, 2015 at 9:13
  • 1
    @Tim It's unclear what the question is. This question isn't asking how to remove duplicates from the bash history — how to do that is stated in the question. This question could be salvaged by editing it to remove that and keep only the question of how to remove duplicates. Jul 14, 2015 at 10:17

3 Answers 3

5

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).

4

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.