-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-history-search.zsh
72 lines (54 loc) · 1.91 KB
/
zsh-history-search.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# check for fzf installed
if ! [ $commands[fzf] ]; then
echo "[zsh-history-search]: couldn't find fzf installation" >&2
echo "[zsh-history-search]: please install fzf in order to use zjump" >&2
fi
function _history_search_widget {
local BUFFER_ORIGIN=${BUFFER}
local CURSOR_ORIGIN=${CURSOR}
BUFFER=''
zle -Rc
echo -n "${fg_bold[magenta]}…${reset_color}"
local query=${BUFFER_ORIGIN} # whole command
local cmd=$(history -n 0 | \
fzf --height 10 --reverse --tac --no-sort --query=${query} --select-1)
if [ -n ${cmd} ]; then
BUFFER="${cmd}"
CURSOR=${#BUFFER}
else
BUFFER=${BUFFER_ORIGIN}
CURSOR=${CURSOR_ORIGIN}
fi
zle redisplay
}
HISTORY_ARGUMENT_SEARCH_LIMIT=1000 # default
function _history_search_argument_widget {
BUFFER_ORIGIN=${BUFFER}
CURSOR_ORIGIN=${CURSOR}
LBUFFER_ORIGIN=${LBUFFER}
RBUFFER_ORIGIN=${RBUFFER}
BUFFER=''
zle -Rc
echo -n "${LBUFFER_ORIGIN%${query}}${fg_bold[magenta]}…${reset_color}${RBUFFER_ORIGIN}"
local query=${${${(z)${:-_ ${LBUFFER_ORIGIN}}}[-1]}#_} # left cursor side argument
if [[ ${LBUFFER_ORIGIN} == *' ' ]] && [[ ! ${query} == *' ' ]]; then
query=''
fi
local argument=$(history -n 0 | tail -n $HISTORY_ARGUMENT_SEARCH_LIMIT | (while read line; do echo ${(j:\n:)${(z)line}}; done) | nl | sort -rk2 | sort -uk2 | sort -nk1 | cut -f2- | \
fzf --height 10 --reverse --tac --no-sort --query=${query} --select-1)
if [ -n ${argumen} ]; then
BUFFER="${LBUFFER_ORIGIN%$query}${argument}${RBUFFER_ORIGIN}"
CURSOR=${#${:-"${LBUFFER_ORIGIN%${query}}${argument}"}}
else
BUFFER=${BUFFER_ORIGIN}
CURSOR=${CURSOR_ORIGIN}
fi
zle redisplay
}
# Key Bindings #################################################################
zle -N _history_search_widget
# bind CTRL + R
bindkey '^R' _history_search_widget
zle -N _history_search_argument_widget
# bind CTRL + SPACE
bindkey '^@' _history_search_argument_widget