Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

zsh history is too short

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...