From 7c64d5a801107249c50e08686cc165582cf5cc4f Mon Sep 17 00:00:00 2001 From: Thomas Preisner Date: Mon, 10 Jul 2017 02:57:48 +0200 Subject: [PATCH] zsh: history: add improved history search functionality --- zsh/history.zsh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/zsh/history.zsh b/zsh/history.zsh index fe9f41d..8fa0ca4 100644 --- a/zsh/history.zsh +++ b/zsh/history.zsh @@ -2,3 +2,28 @@ HISTSIZE=1000000 SAVEHIST=1000000 HISTFILE=~/.zsh/zsh_history + +# ===== functions +# vim-like completion of previous executed commands. Depending on cursor +# position it either behaves like cursor up/down or includes already typed +# text. +zle -N zshrc-vi-history-beginning-search-backward +zshrc-vi-history-beginning-search-backward() +{ + local not_at_beginning_of_line + if [[ $CURSOR -ne 0 ]]; then + not_at_beginning_of_line=yes + fi + + zle history-beginning-search-backward + + # start Vi-mode and stay at the same position (Vi-mode moves one left, + # this counters it). + zle vi-cmd-mode + if [[ -n $not_at_beginning_of_line ]]; then + zle vi-forward-char + fi +} + +# only ^P bind is necessary as ^P enters Vi-Mode and ^N only makes sense after calling ^P +bindkey '^P' zshrc-vi-history-beginning-search-backward