key-bindings.zsh
5241 bytes
1# http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html
2# http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Zle-Builtins
3# http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Standard-Widgets
4
5# Make sure that the terminal is in application mode when zle is active, since
6# only then values from $terminfo are valid
7if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
8 function zle-line-init() {
9 echoti smkx
10 }
11 function zle-line-finish() {
12 echoti rmkx
13 }
14 zle -N zle-line-init
15 zle -N zle-line-finish
16fi
17
18# Use emacs key bindings
19bindkey -e
20
21# [PageUp] - Up a line of history
22if [[ -n "${terminfo[kpp]}" ]]; then
23 bindkey -M emacs "${terminfo[kpp]}" up-line-or-history
24 bindkey -M viins "${terminfo[kpp]}" up-line-or-history
25 bindkey -M vicmd "${terminfo[kpp]}" up-line-or-history
26fi
27# [PageDown] - Down a line of history
28if [[ -n "${terminfo[knp]}" ]]; then
29 bindkey -M emacs "${terminfo[knp]}" down-line-or-history
30 bindkey -M viins "${terminfo[knp]}" down-line-or-history
31 bindkey -M vicmd "${terminfo[knp]}" down-line-or-history
32fi
33
34# Start typing + [Up-Arrow] - fuzzy find history forward
35autoload -U up-line-or-beginning-search
36zle -N up-line-or-beginning-search
37
38bindkey -M emacs "^[[A" up-line-or-beginning-search
39bindkey -M viins "^[[A" up-line-or-beginning-search
40bindkey -M vicmd "^[[A" up-line-or-beginning-search
41if [[ -n "${terminfo[kcuu1]}" ]]; then
42 bindkey -M emacs "${terminfo[kcuu1]}" up-line-or-beginning-search
43 bindkey -M viins "${terminfo[kcuu1]}" up-line-or-beginning-search
44 bindkey -M vicmd "${terminfo[kcuu1]}" up-line-or-beginning-search
45fi
46
47# Start typing + [Down-Arrow] - fuzzy find history backward
48autoload -U down-line-or-beginning-search
49zle -N down-line-or-beginning-search
50
51bindkey -M emacs "^[[B" down-line-or-beginning-search
52bindkey -M viins "^[[B" down-line-or-beginning-search
53bindkey -M vicmd "^[[B" down-line-or-beginning-search
54if [[ -n "${terminfo[kcud1]}" ]]; then
55 bindkey -M emacs "${terminfo[kcud1]}" down-line-or-beginning-search
56 bindkey -M viins "${terminfo[kcud1]}" down-line-or-beginning-search
57 bindkey -M vicmd "${terminfo[kcud1]}" down-line-or-beginning-search
58fi
59
60# [Home] - Go to beginning of line
61if [[ -n "${terminfo[khome]}" ]]; then
62 bindkey -M emacs "${terminfo[khome]}" beginning-of-line
63 bindkey -M viins "${terminfo[khome]}" beginning-of-line
64 bindkey -M vicmd "${terminfo[khome]}" beginning-of-line
65fi
66# [End] - Go to end of line
67if [[ -n "${terminfo[kend]}" ]]; then
68 bindkey -M emacs "${terminfo[kend]}" end-of-line
69 bindkey -M viins "${terminfo[kend]}" end-of-line
70 bindkey -M vicmd "${terminfo[kend]}" end-of-line
71fi
72
73# [Shift-Tab] - move through the completion menu backwards
74if [[ -n "${terminfo[kcbt]}" ]]; then
75 bindkey -M emacs "${terminfo[kcbt]}" reverse-menu-complete
76 bindkey -M viins "${terminfo[kcbt]}" reverse-menu-complete
77 bindkey -M vicmd "${terminfo[kcbt]}" reverse-menu-complete
78fi
79
80# [Backspace] - delete backward
81bindkey -M emacs '^?' backward-delete-char
82bindkey -M viins '^?' backward-delete-char
83bindkey -M vicmd '^?' backward-delete-char
84# [Delete] - delete forward
85if [[ -n "${terminfo[kdch1]}" ]]; then
86 bindkey -M emacs "${terminfo[kdch1]}" delete-char
87 bindkey -M viins "${terminfo[kdch1]}" delete-char
88 bindkey -M vicmd "${terminfo[kdch1]}" delete-char
89else
90 bindkey -M emacs "^[[3~" delete-char
91 bindkey -M viins "^[[3~" delete-char
92 bindkey -M vicmd "^[[3~" delete-char
93
94 bindkey -M emacs "^[3;5~" delete-char
95 bindkey -M viins "^[3;5~" delete-char
96 bindkey -M vicmd "^[3;5~" delete-char
97fi
98
99# [Ctrl-Delete] - delete whole forward-word
100bindkey -M emacs '^[[3;5~' kill-word
101bindkey -M viins '^[[3;5~' kill-word
102bindkey -M vicmd '^[[3;5~' kill-word
103
104# [Ctrl-RightArrow] - move forward one word
105bindkey -M emacs '^[[1;5C' forward-word
106bindkey -M viins '^[[1;5C' forward-word
107bindkey -M vicmd '^[[1;5C' forward-word
108# [Ctrl-LeftArrow] - move backward one word
109bindkey -M emacs '^[[1;5D' backward-word
110bindkey -M viins '^[[1;5D' backward-word
111bindkey -M vicmd '^[[1;5D' backward-word
112
113
114bindkey '\ew' kill-region # [Esc-w] - Kill from the cursor to the mark
115bindkey -s '\el' '^q ls\n' # [Esc-l] - run command: ls
116bindkey '^r' history-incremental-search-backward # [Ctrl-r] - Search backward incrementally for a specified string. The string may begin with ^ to anchor the search to the beginning of the line.
117bindkey ' ' magic-space # [Space] - don't do history expansion
118
119
120# Edit the current command line in $VISUAL (or $EDITOR / `vi` if not set)
121autoload -U edit-command-line
122zle -N edit-command-line
123bindkey '\C-x\C-e' edit-command-line
124
125# file rename magick
126bindkey "^[m" copy-prev-shell-word
127
128# consider emacs keybindings:
129
130#bindkey -e ## emacs key bindings
131#
132#bindkey '^[[A' up-line-or-search
133#bindkey '^[[B' down-line-or-search
134#bindkey '^[^[[C' emacs-forward-word
135#bindkey '^[^[[D' emacs-backward-word
136#
137#bindkey -s '^X^Z' '%-^M'
138#bindkey '^[e' expand-cmd-path
139#bindkey '^[^I' reverse-menu-complete
140#bindkey '^X^N' accept-and-infer-next-history
141#bindkey '^W' kill-region
142#bindkey '^I' complete-word
143## Fix weird sequence that rxvt produces
144#bindkey -s '^[[Z' '\t'
145#