112

When I run history in Bash, I get a load of results (1000+). However, when I run history the zsh shell I only get 15 results. This makes grepping history in zsh mostly useless. My .zshrc file contains the following lines:

HISTFILE=~/.zhistory
HISTSIZE=SAVEHIST=10000
setopt sharehistory
setopt extendedhistory

How can I fix zsh to make my shell history more useful?


UPDATE

If in zsh I call history 1 I get all of my history, just as I do in Bash with history. I could alias the command to get the same result, but I wonder why does history behave differently in zsh and in Bash.

1
  • 2
    As an alternative to grepping through history, I can strongly recommend fzf (github.com/junegunn/fzf) - it allows fuzzy search in your shell history on ctrl+r.
    – David
    Jul 24, 2020 at 14:54

3 Answers 3

148

NVaughan (the OP) has already stated the answer in an update to the question: history behaves differently in bash than it does in zsh:

In short:

  • zsh:
    • history lists only the 15 most recent history entries
    • history 1 lists all - see below.
  • bash:
    • history lists all history entries.

Sadly, passing a numerical operand to history behaves differently, too:

  • zsh:
    • history <n> shows all entries starting with <n> - therefore, history 1 shows all entries.
    • (history -<n> - note the - - shows the <n> most recent entries, so the default behavior is effectively history -15)
  • bash:
    • history <n> shows the <n> most recent entries.
    • (bash's history doesn't support listing from an entry number; you can use fc -l <n>, but a specific entry <n> must exist, otherwise the command fails - see below.)

Optional background info:

  • In zsh, history is effectively (not actually) an alias for fc -l: see man zshbuiltins
    • For the many history-related features, see man zshall
  • In bash, history is its own command whose syntax differs from fc -l
    • See: man bash
  • Both bash and zsh support fc -l <fromNum> [<toNum>] to list a given range of history entries:
    • bash: specific entry <fromNum> must exist.
    • zsh: command succeeds as long as least 1 entry falls in the (explicit or implied) range.
    • Thus, fc -l 1 works in zsh to return all history entries, whereas in bash it generally won't, given that entry #1 typically no longer exists (but, as stated, you can use history without arguments to list all entries in bash).
2
  • 1
    Is there a way to change the default number of lines of output in zsh's history? (other than the obvious alias history="fc -l $NUMBER)
    – Dan Lenski
    Feb 12, 2020 at 23:40
  • 1
    Oh my zsh sets a shell alias history=omz_history which lists all the history. github.com/ohmyzsh/ohmyzsh/blob/…
    – Andrew
    Apr 4, 2022 at 15:54
25
#set history size
export HISTSIZE=10000
#save history after logout
export SAVEHIST=10000
#history file
export HISTFILE=~/.zhistory
#append into history file
setopt INC_APPEND_HISTORY
#save only one command if 2 common are same and consistent
setopt HIST_IGNORE_DUPS
#add timestamp for each entry
setopt EXTENDED_HISTORY   

this is my setting, and it work

4
  • 9
    +1 for useful settings, but it doesn't answer the question (history (without arguments) in zsh only shows the 15 most recent entries, so the problem was a display problem).
    – mklement0
    Nov 10, 2014 at 16:13
  • FYI: bck-i-search (reverse history search in zsh) will search the entire history (>history 1)
    – paiego
    Dec 8, 2020 at 21:12
  • how did you get the timestamp after setopt EXTENDED_HISTORY ? Aug 28, 2021 at 19:41
  • on mac you would still need to alias history to history 1 Jul 26, 2022 at 18:00
13

Perhaps late however coming across this post and trying to apply it, and failed .... so in practical terms, put this in .zshrc :

alias history='history 1'

and you'll see anything until the HIST_SIZE runs out. To find a command I use (after the .zshrc change)

history | grep "my_grep_string"
2
  • 5
    It is always a bit precarious to change the meaning of a command... it might be better to define alias hist='history 1' so the meaning of the original history isn't affected (in case there are scripts etc that use it). In general I like the idea of using an alias to make life easier.
    – Floris
    Mar 30, 2021 at 15:04
  • yes this is required on mac, at least for me Jul 26, 2022 at 18:01

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