T
T
Talifero2015-03-30 11:29:26
linux
Talifero, 2015-03-30 11:29:26

How to set up command history search in ZSH?

When I found zshrc on the network, a command history filter was implemented there, for example:
talifero-HP% apt (further with up and down arrows)
talifero-HP% apt-get install network-manager-openvpn
talifero-HP% apt-get purge mc
talifero-HP% apt-get install ssh

That is, from the history it displays only those commands that begin with apt.
In general, zshrc got lost, I restored everything except for this function. Help plz!!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Talifero, 2015-07-19
@Talifero

This is how it works:

setopt appendhistory
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_REDUCE_BLANKS
setopt HIST_IGNORE_SPACE

setopt INC_APPEND_HISTORY

bindkey '^[OA' history-beginning-search-backward
bindkey '^[OB' history-beginning-search-forward

setopt autocd

autoload -Uz compinit
compinit

setopt extendedglob nomatch notify

zstyle ':completion:*' completer _expand _complete _ignored _correct _approximate
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' use-compctl false
zstyle :compinstall filename '/home/zsh/.zshrc'

zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'

setopt correctall

S
Sergey Petrikov, 2015-03-30
@RicoX

Try like this:

bindkey '^[[A' up-line-or-search                # up arrow for back-history-search
bindkey '^[[B' down-line-or-search              # down arrow for fwd-history-search

In general, when working with history, I personally also use this:
HISTFILE=~/.zhistory
## Число команд, сохраняемых в HISTFILE
SAVEHIST=5000
## Чucлo команд, coxpaняeмыx в сеансе
HISTSIZE=5000
DIRSTACKSIZE=20
# Опции истории команд
#Добавляет в историю время выполнения команды.
setopt extended_history
alias history='fc -il 1'

#История становится общей между всеми сессиями / терминалами.
setopt share_history

# Дополнение файла истрии
setopt  APPEND_HISTORY

#Добавить команду в историю не после выполнения а перед
setopt INC_APPEND_HISTORY

# Игнopupoвaть вce пoвтopeнuя команд
setopt  HIST_IGNORE_ALL_DUPS

# Удалять из файл истории пустые строки
setopt  HIST_REDUCE_BLANKS

# команды «history» и «fc» в историю заноситься не будут
setopt HIST_NO_STORE

M
Maxim Samoilov, 2015-03-30
@Nitive

Sergey's solution does not work for me, I did it like this:

typeset -A key
if [[ "$TERM" != emacs ]]; then
 || bindkey -M viins "$terminfo[kcuu1]" up-line-or-search
 || bindkey -M viins "$terminfo[kcud1]" down-line-or-search
fi
function zle-line-init () { echoti smkx }
function zle-line-finish () { echoti rmkx }
zle -N zle-line-init
zle -N zle-line-finish

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question