Skip to content

Commit d8f7532

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 d8f7532

File tree

2 files changed

+88
-37
lines changed

2 files changed

+88
-37
lines changed

README.md

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,72 +42,77 @@ Using [Oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh):
4242
Usage
4343
------------------------------------------------------------------------------
4444

45-
1. Load this script into your interactive ZSH session:
45+
1. Define the keys to be bind to the `history-substring-search-up` and
46+
`history-substring-search-down` functions from this script. This can be
47+
done using the variables shown below.
4648

47-
% source zsh-history-substring-search.zsh
48-
49-
If you want to use [zsh-syntax-highlighting][6] along with this script,
50-
then make sure that you load it *before* you load this script:
51-
52-
% source zsh-syntax-highlighting.zsh
53-
% source zsh-history-substring-search.zsh
54-
55-
2. Bind keyboard shortcuts to this script's functions.
56-
57-
Users typically bind their UP and DOWN arrow keys to this script, thus:
49+
Users typically bind their UP and DOWN arrow keys to these functions,
50+
thus:
5851
* 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.)
52+
(**NOTE:** In some cases, `cat -v` shows the wrong key codes. If the
53+
key codes shown by `cat -v` don't work for you, press `<CONTROL-v><UP>`
54+
and `<CONTROL-v><DOWN>` at your ZSH command line prompt for correct key
55+
codes.)
6256
* Press the UP arrow key and observe what is printed in your terminal.
6357
* 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`.
58+
* Press the CONTROL and C keys simultaneously to terminate the `cat -v`.
6559
* Use your observations from the previous steps to create key bindings.
6660
For example, if you observed `^[[A` for UP and `^[[B` for DOWN, then:
6761

68-
bindkey '^[[A' history-substring-search-up
69-
bindkey '^[[B' history-substring-search-down
62+
HISTORY_SUBSTRING_SEARCH_UP_MAIN_KEYS=('^[[A')
63+
HISTORY_SUBSTRING_SEARCH_DOWN_MAIN_KEYS=('^[[B')
7064

7165
However, if the observed values don't work, you can try using terminfo:
7266

73-
bindkey "$terminfo[kcuu1]" history-substring-search-up
74-
bindkey "$terminfo[kcud1]" history-substring-search-down
67+
HISTORY_SUBSTRING_SEARCH_UP_MAIN_KEYS=($terminfo[kcuu1])
68+
HISTORY_SUBSTRING_SEARCH_DOWN_MAIN_KEYS=($terminfo[kcud1])
7569

76-
You might also want to bind the Control-P/N keys for use in EMACS mode:
70+
You might also want to bind the CONTROL-P and CONTROL-N keys for use in
71+
EMACS mode:
7772

78-
bindkey -M emacs '^P' history-substring-search-up
79-
bindkey -M emacs '^N' history-substring-search-down
73+
HISTORY_SUBSTRING_SEARCH_UP_EMACS_KEYS=('^P')
74+
HISTORY_SUBSTRING_SEARCH_DOWN_EMACS_KEYS=('^N')
8075

8176
You might also want to bind the `k` and `j` keys for use in VI mode:
8277

83-
bindkey -M vicmd 'k' history-substring-search-up
84-
bindkey -M vicmd 'j' history-substring-search-down
78+
HISTORY_SUBSTRING_SEARCH_UP_VICMD_KEYS=('k')
79+
HISTORY_SUBSTRING_SEARCH_DOWN_VICMD_KEYS=('j')
80+
81+
2. Load this script into your interactive ZSH session:
82+
83+
source zsh-history-substring-search.zsh
84+
85+
If you want to use [zsh-syntax-highlighting][6] along with this script,
86+
then make sure that you load it *before* you load this script:
87+
88+
source zsh-syntax-highlighting.zsh
89+
source zsh-history-substring-search.zsh
8590

8691
3. Type any part of any previous command and then:
8792

8893
* 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
94+
step 1 above, to select the nearest command that (1) contains your query
9095
and (2) is also older than the current command in your command history.
9196

9297
* 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
98+
step 1 above, to select the nearest command that (1) contains your query
9499
and (2) is also newer than the current command in your command history.
95100

96-
* Press `^U` the Control and U keys simultaneously to abort the search.
101+
* Press the CONTROL and U keys simultaneously to abort the search.
97102

98103
4. If a matching command spans more than one line of text, press the LEFT
99104
arrow key to move the cursor away from the end of the command, and then:
100105

101106
* Press the `history-substring-search-up` key, which was configured in
102-
step 2 above, to move the cursor to the line above the cursored line.
107+
step 1 above, to move the cursor to the line above the cursored line.
103108
When the cursor reaches the first line of the command, pressing the
104109
`history-substring-search-up` key again will cause this script to
105110
perform another search.
106111

107112
* Press the `history-substring-search-down` key, which was configured in
108-
step 2 above, to move the cursor to the line below the cursored line.
113+
step 1 above, to move the cursor to the line below the cursored line.
109114
When the cursor reaches the last line of the command, pressing the
110-
`history-substring-search-down` key, which was configured in step 2
115+
`history-substring-search-down` key, which was configured in step 1
111116
above, again will cause this script to perform another search.
112117

113118

@@ -161,6 +166,27 @@ default values.
161166
receive globally unique search results only once, then use this
162167
configuration variable, or use `setopt HIST_IGNORE_ALL_DUPS`.
163168

169+
The following variables can be set before having loaded this script into your
170+
ZSH session to define the `history-substring-search-up` and
171+
`history-substring-search-down` key bindings.
172+
173+
* `HISTORY_SUBSTRING_SEARCH_UP_MAIN_KEYS` is a global array that defines the
174+
main keymap keys to be bind to the `history-substring-search-up` function.
175+
176+
* `HISTORY_SUBSTRING_SEARCH_DOWN_MAIN_KEYS` is a global array that defines the
177+
main keymap keys to be bind to the `history-substring-search-down` function.
178+
179+
* `HISTORY_SUBSTRING_SEARCH_UP_EMACS_KEYS` is a global array that defines the
180+
EMACS mode keys to be bind to the `history-substring-search-up` function.
181+
182+
* `HISTORY_SUBSTRING_SEARCH_DOWN_EMACS_KEYS` is a global array that defines the
183+
EMACS mode keys to be bind to the `history-substring-search-down` function.
184+
185+
* `HISTORY_SUBSTRING_SEARCH_UP_VICMD_KEYS` is a global array that defines the
186+
VI mode keys to be bind to the `history-substring-search-up` function.
187+
188+
* `HISTORY_SUBSTRING_SEARCH_DOWN_VICMD_KEYS` is a global array that defines the
189+
VI mode keys to be bind to the `history-substring-search-down` function.
164190

165191
History
166192
------------------------------------------------------------------------------

zsh-history-substring-search.zsh

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@
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+
typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND=${HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_FOUND:='bg=magenta,fg=white,bold'}
47+
typeset -g HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND=${HISTORY_SUBSTRING_SEARCH_HIGHLIGHT_NOT_FOUND:='bg=red,fg=white,bold'}
48+
typeset -g HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS=${HISTORY_SUBSTRING_SEARCH_GLOBBING_FLAGS:='i'}
49+
typeset -g HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE=${HISTORY_SUBSTRING_SEARCH_ENSURE_UNIQUE:=''}
50+
typeset -g HISTORY_SUBSTRING_SEARCH_FUZZY=${HISTORY_SUBSTRING_SEARCH_FUZZY:=''}
51+
typeset -g HISTORY_SUBSTRING_SEARCH_PREFIXED=${HISTORY_SUBSTRING_SEARCH_PREFIXED:=''}
5252

5353
#-----------------------------------------------------------------------------
5454
# declare internal global variables
@@ -763,5 +763,30 @@ _history-substring-search-down-search() {
763763
fi
764764
}
765765

766+
#-----------------------------------------------------------------------------
767+
# Set key bindings
768+
#-----------------------------------------------------------------------------
769+
770+
local _history_substring_search_key
771+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_UP_MAIN_KEYS; do
772+
bindkey $_history_substring_search_key history-substring-search-up
773+
done
774+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_DOWN_MAIN_KEYS; do
775+
bindkey $_history_substring_search_key history-substring-search-down
776+
done
777+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_UP_EMACS_KEYS; do
778+
bindkey -M emacs $_history_substring_search_key history-substring-search-up
779+
done
780+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_DOWN_EMACS_KEYS; do
781+
bindkey -M emacs $_history_substring_search_key history-substring-search-down
782+
done
783+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_UP_VICMD_KEYS; do
784+
bindkey -M vicmd $_history_substring_search_key history-substring-search-up
785+
done
786+
for _history_substring_search_key in $HISTORY_SUBSTRING_SEARCH_DOWN_VICMD_KEYS; do
787+
bindkey -M vicmd $_history_substring_search_key history-substring-search-down
788+
done
789+
unset _history_substring_search_key
790+
766791
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
767792
# vim: ft=zsh sw=2 ts=2 et

0 commit comments

Comments
 (0)