Skip to content

Commit d8e51d8

Browse files
committed
Bind keys and allow users to override the default ones
This will save users from some common boilerplate code in their own .zshrc file. Fixes #107
1 parent 4abed97 commit d8e51d8

File tree

2 files changed

+117
-46
lines changed

2 files changed

+117
-46
lines changed

README.md

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -44,58 +44,27 @@ Usage
4444

4545
1. Load this script into your interactive ZSH session:
4646

47-
% source zsh-history-substring-search.zsh
47+
source zsh-history-substring-search.zsh
4848

4949
If you want to use [zsh-syntax-highlighting][6] along with this script,
5050
then make sure that you load it *before* you load this script:
5151

52-
% source zsh-syntax-highlighting.zsh
53-
% source zsh-history-substring-search.zsh
52+
source zsh-syntax-highlighting.zsh
53+
source zsh-history-substring-search.zsh
5454

55-
2. Bind keyboard shortcuts to this script's functions.
55+
2. Type any part of any previous command and then:
5656

57-
Users typically bind their UP and DOWN arrow keys to this script, thus:
58-
* Run `cat -v` in your favorite terminal emulator to observe key codes.
59-
     (**NOTE:** In some cases, `cat -v` shows the wrong key codes. If the
60-
key codes shown by `cat -v` don't work for you, press `<C-v><UP>` and
61-
`<C-v><DOWN>` at your ZSH command line prompt for correct key codes.)
62-
* Press the UP arrow key and observe what is printed in your terminal.
63-
* Press the DOWN arrow key and observe what is printed in your terminal.
64-
* Press the Control and C keys simultaneously to terminate the `cat -v`.
65-
* Use your observations from the previous steps to create key bindings.
66-
For example, if you observed `^[[A` for UP and `^[[B` for DOWN, then:
67-
68-
bindkey '^[[A' history-substring-search-up
69-
bindkey '^[[B' history-substring-search-down
70-
71-
However, if the observed values don't work, you can try using terminfo:
72-
73-
bindkey "$terminfo[kcuu1]" history-substring-search-up
74-
bindkey "$terminfo[kcud1]" history-substring-search-down
75-
76-
You might also want to bind the Control-P/N keys for use in EMACS mode:
77-
78-
bindkey -M emacs '^P' history-substring-search-up
79-
bindkey -M emacs '^N' history-substring-search-down
80-
81-
You might also want to bind the `k` and `j` keys for use in VI mode:
82-
83-
bindkey -M vicmd 'k' history-substring-search-up
84-
bindkey -M vicmd 'j' history-substring-search-down
85-
86-
3. Type any part of any previous command and then:
87-
88-
* Press the `history-substring-search-up` key, which was configured in
89-
step 2 above, to select the nearest command that (1) contains your query
57+
* Press the `history-substring-search-up` key, which is the UP key by
58+
default, to select the nearest command that (1) contains your query
9059
and (2) is also older than the current command in your command history.
9160

92-
* Press the `history-substring-search-down` key, which was configured in
93-
step 2 above, to select the nearest command that (1) contains your query
61+
* Press the `history-substring-search-down` key, which is the DOWN key by
62+
default, to select the nearest command that (1) contains your query
9463
and (2) is also newer than the current command in your command history.
9564

9665
* Press `^U` the Control and U keys simultaneously to abort the search.
9766

98-
4. If a matching command spans more than one line of text, press the LEFT
67+
3. If a matching command spans more than one line of text, press the LEFT
9968
arrow key to move the cursor away from the end of the command, and then:
10069

10170
* Press the `history-substring-search-up` key, which was configured in
@@ -161,6 +130,51 @@ default values.
161130
receive globally unique search results only once, then use this
162131
configuration variable, or use `setopt HIST_IGNORE_ALL_DUPS`.
163132

133+
The following variables must be overridden before having loaded this script
134+
into your ZSH session, and they define the `history-substring-search-up` and
135+
`history-substring-search-down` key bindings.
136+
137+
* `HISTORY_SUBSTRING_SEARCH_MAIN_UP_KEYS` is a global array that defines the
138+
main keymap keys to be bind to the `history-substring-search-up` command.
139+
Its default value is `('^[[A' $terminfo[kcuu1])`, which are two common codes
140+
for the UP key in most terminals.
141+
142+
* `HISTORY_SUBSTRING_SEARCH_MAIN_DOWN_KEYS` is a global array that defines the
143+
main keymap keys to be bind to the `history-substring-search-down` command.
144+
Its default value is `('^[[B' $terminfo[kcud1])`, which are two common codes
145+
for the DOWN key in most terminals.
146+
147+
* `HISTORY_SUBSTRING_SEARCH_EMACS_UP_KEYS` is a global array that defines the
148+
EMACS mode keys to be bind to the `history-substring-search-up` command.
149+
Its default value is `('^P')`, which is the code for the Control-P key.
150+
151+
* `HISTORY_SUBSTRING_SEARCH_EMACS_DOWN_KEYS` is a global array that defines the
152+
EMACS mode keys to be bind to the `history-substring-search-down` command.
153+
Its default value is `('^N')`, which is the code for the Control-N key.
154+
155+
* `HISTORY_SUBSTRING_SEARCH_VICMD_UP_KEYS` is a global array that defines the
156+
VI mode keys to be bind to the `history-substring-search-up` command.
157+
Its default value is `('k')`, which is the code for the `k` key.
158+
159+
* `HISTORY_SUBSTRING_SEARCH_VICMD_DOWN_KEYS` is a global array that defines the
160+
VI mode keys to be bind to the `history-substring-search-down` command.
161+
Its default value is `('j')`, which is the code for the `j` key.
162+
163+
Users typically bind their UP and DOWN arrow keys to this script. If the
164+
defaults provided above don't work for you, then:
165+
* Run `cat -v` in your favorite terminal emulator to observe key codes.
166+
(**NOTE:** In some cases, `cat -v` shows the wrong key codes. If the
167+
key codes shown by `cat -v` don't work for you, press `<Control-v><UP>` and
168+
`<Control-v><DOWN>` at your ZSH command line prompt for correct key codes.)
169+
* Press the UP arrow key and observe what is printed in your terminal.
170+
* Press the DOWN arrow key and observe what is printed in your terminal.
171+
* Press the Control and C keys simultaneously to terminate the `cat -v`.
172+
* Use your observations from the previous steps to create key bindings.
173+
For example, if you observed `^[OA` for UP and `^[OB` for DOWN, then:
174+
175+
HISTORY_SUBSTRING_SEARCH_MAIN_UP_KEYS=('^[OA')
176+
HISTORY_SUBSTRING_SEARCH_MAIN_DOWN_KEYS=('^[OB')
177+
source zsh-history-substring-search.zsh
164178

165179
History
166180
------------------------------------------------------------------------------

zsh-history-substring-search.zsh

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,44 @@
4343
# declare global configuration variables
4444
#-----------------------------------------------------------------------------
4545

46-
: ${HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold'}
47-
: ${HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'}
48-
: ${HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'}
49-
: ${HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''}
50-
: ${HISTORY_SUBSTRING_SEARCH_FUZZY=''}
51-
: ${HISTORY_SUBSTRING_SEARCH_PREFIXED=''}
46+
if (( ! $+HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND )); then
47+
typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND='bg=magenta,fg=white,bold'
48+
fi
49+
if (( ! $+HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND )); then
50+
typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND='bg=red,fg=white,bold'
51+
fi
52+
if (( ! $+HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS )); then
53+
typeset -g HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS='i'
54+
fi
55+
if (( ! $+HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE )); then
56+
typeset -g HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=''
57+
fi
58+
if (( ! $+HISTORY_SUBSTRING_SEARCH_FUZZY )); then
59+
typeset -g HISTORY_SUBSTRING_SEARCH_FUZZY=''
60+
fi
61+
if (( ! $+HISTORY_SUBSTRING_SEARCH_PREFIXED )); then
62+
typeset -g HISTORY_SUBSTRING_SEARCH_PREFIXED=''
63+
fi
64+
65+
zmodload -F zsh/terminfo +p:terminfo
66+
if (( ! $+HISTORY_SUBSTRING_SEARCH_MAIN_UP_KEYS )); then
67+
typeset -ga HISTORY_SUBSTRING_SEARCH_MAIN_UP_KEYS=('^[[A' $terminfo[kcuu1])
68+
fi
69+
if (( ! $+HISTORY_SUBSTRING_SEARCH_MAIN_DOWN_KEYS )); then
70+
typeset -ga HISTORY_SUBSTRING_SEARCH_MAIN_DOWN_KEYS=('^[[B' $terminfo[kcud1])
71+
fi
72+
if (( ! $+HISTORY_SUBSTRING_SEARCH_EMACS_UP_KEYS )); then
73+
typeset -ga HISTORY_SUBSTRING_SEARCH_EMACS_UP_KEYS=('^P')
74+
fi
75+
if (( ! $+HISTORY_SUBSTRING_SEARCH_EMACS_DOWN_KEYS )); then
76+
typeset -ga HISTORY_SUBSTRING_SEARCH_EMACS_DOWN_KEYS=('^N')
77+
fi
78+
if (( ! $+HISTORY_SUBSTRING_SEARCH_VICMD_UP_KEYS )); then
79+
typeset -ga HISTORY_SUBSTRING_SEARCH_VICMD_UP_KEYS=('k')
80+
fi
81+
if (( ! $+HISTORY_SUBSTRING_SEARCH_VICMD_DOWN_KEYS )); then
82+
typeset -ga HISTORY_SUBSTRING_SEARCH_VICMD_DOWN_KEYS=('j')
83+
fi
5284

5385
#-----------------------------------------------------------------------------
5486
# declare internal global variables
@@ -763,5 +795,30 @@ _history-substring-search-down-search() {
763795
fi
764796
}
765797

798+
#-----------------------------------------------------------------------------
799+
# Set key bindings
800+
#-----------------------------------------------------------------------------
801+
802+
local _history_substring_search_key
803+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_MAIN_UP_KEYS; do
804+
bindkey $_history_substring_search_key history-substring-search-up
805+
done
806+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_MAIN_DOWN_KEYS; do
807+
bindkey $_history_substring_search_key history-substring-search-down
808+
done
809+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_EMACS_UP_KEYS; do
810+
bindkey -M emacs $_history_substring_search_key history-substring-search-up
811+
done
812+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_EMACS_DOWN_KEYS; do
813+
bindkey -M emacs $_history_substring_search_key history-substring-search-down
814+
done
815+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_VICMD_UP_KEYS; do
816+
bindkey -M vicmd $_history_substring_search_key history-substring-search-up
817+
done
818+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_VICMD_DOWN_KEYS; do
819+
bindkey -M vicmd $_history_substring_search_key history-substring-search-down
820+
done
821+
unset _history_substring_search_key
822+
766823
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
767824
# vim: ft=zsh sw=2 ts=2 et

0 commit comments

Comments
 (0)