Zsh 的历史太短了

当我在 Bash 中运行 history时,会得到大量的结果(1000 +)。但是,当我运行 history和 zsh shell 时,我只得到15个结果。这使得在 zsh 中抓取历史记录几乎毫无用处。 我的 .zshrc文件包含以下几行:

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

我如何修复 zsh 使我的 shell 历史更有用?


更新

如果在 zsh 中调用 history 1,我将获得所有的历史记录,就像在 Bash 中使用 history一样。我可以使用别名命令来得到相同的结果,但是我想知道为什么 history在 zsh 和 Bash 中的行为不同。

38973 次浏览
#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

NVaughan (the OP) has already stated the answer in an update to the question: ABC0 behaves differently in ABC1 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).

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"