32

I'm looking for a setting that will make it so that when i hit the up arrow, zsh shows commands i have recently edited. The catch is, i only want unique commands. Currently, if i type echo "hello world" 50 times, i have to press up arrow 50 times to get the command i used before typing the echo command. This is annoying to say the least.

Any thoughts on what setting i need to enable/disable?

peth
  • 9,890
  • 3
  • 34
  • 41
Lee Olayvar
  • 463
  • 5
  • 6

2 Answers2

37

HIST_IGNORE_ALL_DUPS will throw out all previous matches of the command, which can be confusing when using the history as a log of what you did later:

HIST_IGNORE_ALL_DUPS: If a new command line being added to the history list duplicates an older one, the older command is removed from the list (even if it is not the previous event).

A closer fit to your needs is probably the HIST_FIND_NO_DUPS option:

HIST_FIND_NO_DUPS: When searching for history entries in the line editor, do not display duplicates of a line previously found, even if the duplicates are not contiguous.

Or maybe the HIST_IGNORE_DUPS, but, as RichieHH notes, it also leads to an incomplete account of history:

HIST_IGNORE_DUPS: Do not enter command lines into the history list if they are duplicates of the previous event.

See: man zshoptions | less -p History.

peth
  • 9,890
  • 3
  • 34
  • 41
  • 2
    HIST_FIND_NO_DUPS is a better answer than mine I think. – Mikel Apr 20 '11 at 21:27
  • 2
    Bash: `export HISTCONTROL=ignoredups`. ZSH: `setopt HIST_IGNORE_DUPS` – spyle Mar 11 '20 at 17:39
  • No, HIST_IGNORE_DUPS is not the best as it leaves the old instance as opposed to "refreshing it" in history. HIST_FIND_NO_DUPS is good. HIST_IGNORE_ALL_DUPS *better* because it removes the oldest instance and inserts the newest. Way better to use history to remember where you were! – RichieHH Nov 20 '20 at 11:17
4

I can't see any way to literally only do that, but if you set the HIST_IGNORE_ALL_DUPS option, only the most recent version of a command will be retained in history, giving you the same effect.

See man zshoptions for details.

Mikel
  • 8,846
  • 1
  • 41
  • 37