4

Do you have an experience like this?

When you try to find a previos used command on terminal by up arrow and you find

$ls         # 1st time push `up arrow`
$ls         # 2nd time push `up arrow`
$ls         # 3rd time push `up arrow`
$ls         # 4th time push `up arrow`
$ls         # 5th time push `up arrow`
$ls         # 6th time push `up arrow`
$make       # 7th time push `up arrow`
$make       # 8th time push `up arrow`
$make       # 9th time push `up arrow`
$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"   # Bingo!

better if it like this

$ls         # 1st time push `up arrow`
$make       # 2th time push `up arrow`
$ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"   # Bingo!

because the duplicated history usually no use.

Any idea?

3 Answers 3

4

You can accomplish this by setting ignoredups in the HISTCONTROL environment variable:

export HISTCONTROL="ignoredups"

From the bash manpage:

   HISTCONTROL
          A  colon-separated list of  values controlling how commands are 
          saved on the history list. If the list of values includes ignorespace, 
          lines which begin with a space character are not saved in the  history 
          list.   A  value  of ignoredups  causes  lines  matching the previous 
          history entry to not be saved.  A value of ignoreboth is shorthand
          for ignorespace and ignoredups.  A value of erasedups causes all
          previous lines matching the  current  line  to  be removed  from the 
          history list before that line is saved.  Any value not in the above 
          list is ignored.  If HISTCONTROL is unset, or does not include a valid 
          value, all lines read by the shell parser are saved on the history 
          list, subject  to  the  value  of  HISTIGNORE.   The second and 
          subsequent lines of a multi-line compound command are not tested,
          and are added to the history regardless of the value of
          HISTCONTROL.
1
  • While this is a step in the right direction it won't do quite what the OP wants. It will NOT give them just three entries, one for make, one for ls and one for ruby ... it will just bunch up subsequent repeats.
    – tink
    Feb 3, 2015 at 0:50
1

To add to @jordanm reply, I think you should use HISTCONTROL in fact but with the "erasedups" value.

"A value of erasedups causes all previous lines matching the current line to be removed from the history list before that line is saved."

export HISTCONTROL="ignoreboth:erasedups"

Add it to your ~/.bashrc to have it execute every time you log in.

Actually, this has been answered before

I wanted to add a comment but I can't because of my reputation.

0

For macos, add the following lines to your ~/.zshrc file to avoid command duplicates in terminal history

setopt HIST_EXPIRE_DUPS_FIRST
setopt HIST_IGNORE_DUPS
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_IGNORE_SPACE
setopt HIST_FIND_NO_DUPS
setopt HIST_SAVE_NO_DUPS
2
  • beep boop, HIST_BEEP doesn't have anything to do with duplication removal. Dec 20, 2020 at 10:00
  • @比尔盖子 updated the answer
    – VVK
    Dec 20, 2020 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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