Unlimited bash/zsh history
In bash or zsh, the history is my documentation. I have an unlimited history, or at least a super huge history. Once I typed a command into my terminal, on that very machine, it will be there forever or at least for a very long time.
And I have nice autocompletion. If I have ever typed some commands, say cmake -S . -B build and cmake --build build.
If I type cmake + ArrowUp key, it will autocomplete to cmake --build build.
Another ArrowUp will autocomplete to cmake -S . -B build.
You get the point. It takes the written letters and searches through the history for all commands that started with the letters typed.
I have explained that bash/zsh setup often enough to finally write it down.
zsh
This is what I have in my .zshrc file
#use a history file in here
HISTFILE=${ZDOTDIR:-$HOME}/.zsh_history
# make it huge, really huge.
SAVEHIST=1000000
HISTSIZE=1000000
# there is for sure still some redundancy, but ...
# setopt BANG_HIST # Treat the '!' character specially during expansion.
# setopt EXTENDED_HISTORY # Write the history file in the ":start:elapsed;command" format.
setopt INC_APPEND_HISTORY # Write to the history file immediately, not when the shell exits.
setopt SHARE_HISTORY # Share history between all sessions.
setopt HIST_EXPIRE_DUPS_FIRST # Expire duplicate entries first when trimming history.
setopt HIST_IGNORE_DUPS # Don't record an entry that was just recorded again.
setopt HIST_IGNORE_ALL_DUPS # Delete old recorded entry if new entry is a duplicate.
setopt HIST_FIND_NO_DUPS # Do not display a line previously found.
setopt HIST_IGNORE_SPACE # Don't record an entry starting with a space.
setopt HIST_SAVE_NO_DUPS # Don't write duplicate entries in the history file.
setopt HIST_REDUCE_BLANKS # Remove superfluous blanks before recording entry.
#setopt HIST_VERIFY # Don't execute immediately upon history expansion.
#setopt HIST_BEEP # Beep when accessing nonexistent history.
alias history="history 0"
and this in my .zprofile
# this puts the cursor at the end?
autoload -U history-search-end
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
bindkey "^[[A" history-beginning-search-backward-end
bindkey "^[[B" history-beginning-search-forward-end
bash
My bash setup is not as detailed but also works
In my .bash_profile I have
# unlimited history
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT=""
# no duplicates
export HISTCONTROL=ignoreboth:erasedups
# use a specific file
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
PROMPT_COMMAND="history -a; history -c; history -r"
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
For navigating the history with autocompletion there is this in my .bashrc
bind '"\e[A": history-search-backward'
bind '"\e[B": history-search-forward'
Possible improvements?
I have none. The command I typed 3 months ago… what was it? It’s in the history. If I remember the first letters I even have a nice search for it. I am happy.
But if you know about any improvements that could be added, please let me know. Just put them in the comment section below. Thanks in advance!
If you find this page useful, donate a coffee to the C++ user group of Stockholm, SwedenCpp