跳到主要内容

⚙️ History search for multiple words

z-shell/h-s-mw

The plugin allows to search history for multiple keywords, Ctrl+R initiates the search and matched keywords will be found and highlighted.

Install H-S-MW

Add the following to your .zshrc file:

~/.zshrc
zi light z-shell/H-S-MW

Reload the shell with exec zsh or open a new terminal.

Customizing

Context viewing

Add zstyle to ~/.zshrc: zstyle :plugin:history-search-multi-word <value>, where <value> is one of:

ValueDescription
reset-prompt-protect 1See all occurrences of a command together with surrounding commands
page-size "8"Number of entries to show (default is $LINES/3)
page-size "LINES/4"Pages size relative to screen height
highlight-color "fg=yellow,bold"Color to highlight matched, searched text (default bg=17 on 256-color)
synhl "yes"Whether to perform syntax highlighting (default true)
active "underline"Effect on active history entry. Try standout, bold, bg=blue (default underline)
check-paths "yes"Whether to check paths for existence and mark with magenta (default true)
clear-on-cancel "no"Whether pressing Ctrl+c or ESC should clearly enter query (default true)

Example:

~/.zshrc
zstyle :plugin:history-search-multi-word reset-prompt-protect 1
zstyle ":history-search-multi-word" page-size "8"
提示

For a better experience adjust history using options, for example:

~/.zshrc
setopt extended_history       # record timestamp of command in HISTFILE
setopt hist_expire_dups_first # delete duplicates first when HISTFILE size exceeds HISTSIZE
setopt hist_ignore_all_dups # remove older duplicate entries from the history
setopt hist_ignore_dups # ignore duplicated commands history list
setopt hist_ignore_space # ignore commands that start with space
setopt hist_reduce_blanks # remove superfluous blanks from history items
setopt hist_save_no_dups # do not write a duplicate event to the history file
setopt inc_append_history # allow multiple terminal sessions to append to one history
setopt inc_append_history # write to the history file immediately, not when the shell exits.
setopt share_history # share command history data

Features

Refreshing prompt

Use zle reset-prompt in sched calls, in the presence of z-shell/F-Sy-H, zsh-users/zsh-syntax-highlighting, zsh-users/zsh-autosuggestions, and other plugins that hook up into Z-Shell by overloading ZLE widgets.

For example, to refresh the clock in prompt every second:

PROMPT=%B%F{yellow}%D{%H:%M:%S}%B%b%f

schedprompt() {
zle && zle reset-prompt
sched +1 schedprompt
}

zmodload -i zsh/sched
schedprompt

The reset-prompt-protect zstyle needs to be set to 1 for correct cooperation with H-S-MW. Alternatively, you could use zle .reset-prompt (i.e. with the dot in front) to call the original, not an overloaded reset-prompt widget (created by z-shell/F-Sy-H, zsh-users/zsh-autosuggestions, etc.).

Customizing syntax highlighting

Syntax highlighting is customized via the HSMW_HIGHLIGHT_STYLES associative array. It has keys like reserved-word, alias, command, path, etc. which are assigned with strings like fg=blue,bold, to configure how given elements are to be colored. The complete list of available keys is at the beginning of hsmw-highlight.

If you assign this array in ~/.zshrc before or after loading H-S-MW you will change the defaults.

Examples of customizing syntax highlighting

Sets path key – paths that exist will be highlighted with background magenta, foreground white, bold:

typeset -gA HSMW_HIGHLIGHT_STYLES
HSMW_HIGHLIGHT_STYLES[path]="bg=magenta,fg=white,bold"

Enable coloring of options of the form "-o" and "--the option", with cyan:

typeset -gA HSMW_HIGHLIGHT_STYLES
HSMW_HIGHLIGHT_STYLES[single-hyphen-option]="fg=cyan"
HSMW_HIGHLIGHT_STYLES[double-hyphen-option]="fg=cyan"

Use 256 colors to highlight command separators (like ";" or "&&"):

HSMW_HIGHLIGHT_STYLES[commandseparator]="fg=241,bg=17"
Blacklisting paths

Hash holding paths that shouldn't be grepped (globbed) – blacklist for slow disks, mounts, etc.:

typeset -gA FAST_BLIST_PATTERNS
FAST_BLIST_PATTERNS[/mount/nfs1/*]=1
FAST_BLIST_PATTERNS[/mount/disk2/*]=1