DEV Community

Cover image for Can't recall everything you type?
Sall
Sall

Posted on • Updated on

Can't recall everything you type?

Logo


Step 1

Install ❮ ZI ❯ or any other plugin manager for Zsh.

Step 2

Install H-S-MW - it has a feature called context viewing. It also will bind Ctrl-R to a widget that searches for multiple keywords in AND fashion. In other words, you can enter multiple words, and history entries that match all of them will be found. The entries are syntax highlighted. In most cases it provides a sense as commands would be suggested by AI and if your history is optimized in most cases it takes 1 to 3 key presses to command get suggested - which actually been typed months ago and even if only typed once.

Example to optimize history in Zsh. Place to your .zshrc:

# Remove older duplicate entries from history.
setopt hist_ignore_all_dups
# Expire A Duplicate Event First When Trimming History.
setopt hist_expire_dups_first
# Do Not Record An Event That Was Just Recorded Again.
setopt hist_ignore_dups  
# Remove superfluous blanks from history items.       
setopt hist_reduce_blanks
# Do Not Display A Previously Found Event.
setopt hist_find_no_dups
# Do Not Record An Event Starting With A Space.
setopt hist_ignore_space
# Do Not Write A Duplicate Event To The History File.
setopt hist_save_no_dups
# Do Not Execute Immediately Upon History Expansion.        
setopt hist_verify
# Allow multiple sessions append to one zsh command history.           
setopt append_history
# Show Timestamp In History.
setopt extended_history
# Write to history file immediately, not after shell exit.
setopt inc_append_history
# Share history between different instances of the shell.
setopt share_history    

Step 3

Customize it to suit your needs. (add following to your .zshrc)

# Number of entries to show (default is $LINES/3).
zstyle ":history-search-multi-word" page-size "8"                      
# Color in which to highlight matched, 
# searched text (default bg=17 on 256-color terminals)
zstyle ":history-search-multi-word" highlight-color "fg=yellow,bold"
# Whether to perform syntax highlighting (default true)
zstyle ":plugin:history-search-multi-word" synhl "yes"                 
# Effect on active history entry.
# Try: standout, bold, bg=blue (default underline)
zstyle ":plugin:history-search-multi-word" active "underline"
# Whether to check paths for existence
# and mark with magenta (default true)
zstyle ":plugin:history-search-multi-word" check-paths "yes"           
# Whether pressing Ctrl-C or
# ESC should clear entered query
zstyle ":plugin:history-search-multi-word" clear-on-cancel "no"

How many keystrokes does it take to find the command you were looking for? :)

Explore more: https://z-shell.pages.dev

Latest comments (0)