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.
ZSH is a popular shell built on top of bash. It stores your command history in the . zsh_history file in your home directory. If your ZSH shell does not support command history by default, check out our zsh command history article to learn how to enable it.
Zsh is built on top of bash thus it has additional features. Zsh is the default shell for macOS and Kali Linux. Zsh provides the user with more flexibility by providing various features such as plug-in support, better customization, theme support, spelling correction, etc.
Use the history command to list commands that you have previously entered. The history command is a Korn shell built-in command that lists the last 16 commands entered. The Korn shell saves commands that you entered to a command history file, usually named $HOME/. sh_history.
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:
history
lists only the 15 most recent history entrieshistory 1
lists all - see below.history
lists all history entries. Sadly, passing a numerical operand to history
behaves differently, too:
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
)history <n>
shows the <n>
most recent entries.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:
history
is effectively (not actually) an alias for fc -l
: see man zshbuiltins
man zshall
history
is its own command whose syntax differs from fc -l
man bash
fc -l <fromNum> [<toNum>]
to list a given range of history entries: <fromNum>
must exist.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).#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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With