Move into an external dir
[zsh.git] / .zsh / 80_KeyBindings.zsh
1 # -*- mode: sh;-*-
2 ###
3 # Key Bindings
4 zmodload zsh/terminfo
5
6 typeset -gA key_info
7 key_info=(
8 'Control' '\C-'
9 'Escape' '\e'
10 'Meta' '\M-'
11 'Backspace' "^?"
12 'Delete' "^[[3~"
13 'F1' "$terminfo[kf1]"
14 'F2' "$terminfo[kf2]"
15 'F3' "$terminfo[kf3]"
16 'F4' "$terminfo[kf4]"
17 'F5' "$terminfo[kf5]"
18 'F6' "$terminfo[kf6]"
19 'F7' "$terminfo[kf7]"
20 'F8' "$terminfo[kf8]"
21 'F9' "$terminfo[kf9]"
22 'F10' "$terminfo[kf10]"
23 'F11' "$terminfo[kf11]"
24 'F12' "$terminfo[kf12]"
25 'Insert' "$terminfo[kich1]"
26 'Home' "$terminfo[khome]"
27 'PageUp' "$terminfo[kpp]"
28 'End' "$terminfo[kend]"
29 'PageDown' "$terminfo[knp]"
30 'Up' "$terminfo[kcuu1]"
31 'Left' "$terminfo[kcub1]"
32 'Down' "$terminfo[kcud1]"
33 'Right' "$terminfo[kcuf1]"
34 'BackTab' "$terminfo[kcbt]"
35 )
36 # Set empty $key_info values to an invalid UTF-8 sequence to induce silent
37 # bindkey failure.
38 for key in "${(k)key_info[@]}"; do
39 if [[ -z "$key_info[$key]" ]]; then
40 key_info["$key"]='�'
41 fi
42 done
43
44 # Make sure the terminal is in application mode, when zle is
45 # active. Only then are the values from $terminfo valid.
46 if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
47 function zle-smkx () {
48 emulate -L zsh
49 printf '%s' ${terminfo[smkx]}
50 }
51 function zle-rmkx () {
52 emulate -L zsh
53 printf '%s' ${terminfo[rmkx]}
54 }
55 function zle-line-init () {
56 zle-smkx
57 }
58 function zle-line-finish () {
59 zle-rmkx
60 }
61 zle -N zle-line-init
62 zle -N zle-line-finish
63 fi
64
65 # First set it back to known defaults
66 bindkey -d
67
68 # now, our main map is the emacs style
69 bindkey -e
70
71 # First we define widgets that we later bind to keys
72 # This depends on a zle widget by Max Mikhanosha which he posted to
73 # zsh-users at http://www.zsh.org/mla/users/2008/msg00708.html
74 define-pipe-widget insert_grep "grep \"@@@\"" "grep -i \"@@@\"" "grep -v \"@@@\"" "grep @@@"
75 define-pipe-widget insert_head "head" "head\n"
76 define-pipe-widget insert_less "less @@@" "less\n"
77
78 zstyle ':completion:hist-complete:*' completer _history
79
80 zle -N beginning-of-somewhere beginning-or-end-of-somewhere
81 zle -N end-of-somewhere beginning-or-end-of-somewhere
82 zle -N history-beginning-search-backward-end history-search-end
83 zle -N history-beginning-search-forward-end history-search-end
84 zle -C hist-complete complete-word _generic
85
86 for widget in insert-datestamp sudo-command-line jump_after_first_word \
87 inplaceMkDirs magic-single-quotes magic-double-quotes \
88 magic-parentheses magic-square-brackets magic-curly-brackets \
89 magic-angle-brackets predict-on slash-backward-kill-word \
90 insert-files insert-unicode-char edit-command-line \
91 history-beginning-search-menu
92 do
93 debug "Enabling zle widget: ${widget}"
94 zle -N ${widget}
95 done
96
97 # control+j: add new-line for multi-line edits in zle (needs exception for midnight commander)
98 if [[ "$MC_SID" != "" || "$MC_CONTROL_PID" != "" ]]; then
99 bindkey "^J" accept-line
100 else
101 bindkey "^J" self-insert
102 accept-line
103 fi
104
105 bind2maps emacs -- Home beginning-of-somewhere
106 bind2maps viins vicmd -- Home vi-beginning-of-line
107 bind2maps emacs -- End end-of-somewhere
108 bind2maps viins vicmd -- End vi-end-of-line
109 bind2maps emacs viins -- Insert overwrite-mode
110 bind2maps vicmd -- Insert vi-insert
111 bind2maps emacs -- Delete delete-char
112 bind2maps viins vicmd -- Delete vi-delete-char
113 bind2maps emacs viins vicmd -- Up history-substring-search-up
114 bind2maps emacs viins vicmd -- Down history-substring-search-down
115 bind2maps emacs -- Left backward-char
116 bind2maps viins vicmd -- Left vi-backward-char
117 bind2maps emacs -- Right forward-char
118 bind2maps viins vicmd -- Right vi-forward-char
119 bind2maps viins vicmd -- Right vi-forward-char
120 bind2maps emacs -- Backspace backward-delete-char
121 bind2maps viins vicmd -- Backspace vi-backward-delete-char
122
123 #k# search history backward for entry beginning with typed text
124 bind2maps emacs viins -- PageUp history-beginning-search-backward-end
125 #k# search history forward for entry beginning with typed text
126 bind2maps emacs viins -- PageDown history-beginning-search-forward-end
127
128 # Setup some more bindings to be more like emacs.
129 bind2maps emacs -- -s "$key_info[Escape]b" emacs-backward-word
130 bind2maps viins vicmd -- -s "$key_info[Escape]b" vi-backward-word
131 bind2maps emacs -- -s "$key_info[Escape]f" emacs-forward-word
132 bind2maps viins vicmd -- -s "$key_info[Escape]f" vi-forward-word
133 bind2maps emacs -- -s "$key_info[Escape]$key_info[Left]" emacs-backward-word
134 bind2maps emacs -- -s "$key_info[Escape]$key_info[Right]" emacs-forward-word
135 #k# Kill to the beginning of the line.
136 bind2maps emacs viins vicmd -- -s "$key_info[Escape]k" backward-kill-line
137 #k# Redo.
138 bind2maps emacs -- -s "$key_info[Escape]_" redo
139 #k# suspend current line
140 bind2maps emacs viins vicmd -- -s "$key_info[Control]z" push-input
141 #k# Bind Shift + Tab to go to the previous menu item.
142 bind2maps emacs viins vicmd -- -s "$key_info[BackTab]" reverse-menu-complete
143 #k# Insert a timestamp on the command line (yyyy-mm-dd)
144 bind2maps emacs viins -- -s "$key_info[Control]ed" insert-datestamp
145 #k# Put the current command line into a \kbd{sudo} call
146 bind2maps emacs viins -- -s "$key_info[Control]os" sudo-command-line
147 #k# Copy the previous shell word - magic to rename files
148 bind2maps emacs viins -- -s "$key_info[Escape]m" copy-prev-shell-word
149 #k# mkdir -p <dir> from string under cursor or marked area
150 bind2maps emacs viins -- -s "$key_info[Control]xM" inplaceMkDirs
151 #k# Show what the completion system is trying to complete with at a given point
152 bind2maps emacs viins -- -s "$key_info[Control]Xh" _complete_help
153 bind2maps emacs viins -- -s ' ' magic-space
154 #k# Kill left-side word or everything up to next slash
155 bind2maps emacs viins -- -s "$key_info[Escape]v" slash-backward-kill-word
156 #k# Kill left-side word or everything up to next slash
157 bind2maps emacs viins -- -s "$key_info[Escape]$key_info[Backspace]" slash-backward-kill-word
158 #k# Kill left-side word or everything up to next slash
159 bind2maps emacs viins -- -s "$key_info[Escape]$key_info[Delete]" slash-backward-kill-word
160
161 #k# Trigger menu-complete
162 bind2maps emacs viins -- -s '\ei' menu-complete
163 #k# jump to after first word (for adding options)
164 bind2maps emacs viins -- -s '^x1' jump_after_first_word
165 #k# complete word from history with menu
166 bind2maps emacs viins -- -s "^x^x" hist-complete
167 # insert unicode character
168 # usage example: 'ctrl-x i' 00A7 'ctrl-x i' will give you an <A7>
169 # See for example http://unicode.org/charts/ for unicode characters code
170 #k# Insert Unicode character
171 bind2maps emacs viins -- -s '^xi' insert-unicode-char
172 #k# Toggle per directory history
173 bind2maps emacs viins vicmd -- -s '^X^G' per-directory-history-toggle-history
174 #k# search history backward for entry matching typed text
175 bind2maps emacs -- -s "$key_info[Control]P" history-substring-search-up
176 bind2maps vicmd -- -s "k" history-substring-search-up
177 #k# search history forward for entry matching typed text
178 bind2maps emacs -- -s "$key_info[Control]N" history-substring-search-down
179 bind2maps vicmd -- -s "j" history-substring-search-down
180
181 #k# search history and present a menu
182 bind2maps emacs vicmd -- -s "$key_info[Control]X$key_info[Control]X" history-beginning-search-menu
183
184 #k# Trigger zsh anything.el
185 if which zaw >/dev/null; then
186 bind2maps emacs viins -- -s "$key_info[Control]X;" zaw
187 fi
188
189 #k# Insert files and test globbing
190 is4 && bind2maps emacs viins -- -s "$key_info[Control]Xf" insert-files
191 #k# Edit the current line in \kbd{\$EDITOR}
192 is4 && bind2maps emacs viins -- -s "$key_info[Control]x$key_info[Control]e" edit-command-line
193 #k# Magic history searching
194 is4 && bind2maps emacs viins -- -s "$key_info[Control]X$key_info[Control]R" predict-on
195 #bindkey "^U" predict-off ## C-u
196
197 if [[ -z ${MC_SID} ]]; then
198 #k# Append grep, multiple toggle options
199 bind2maps emacs viins -- -s "$key_info[Control]g" insert_grep
200 #k# Append head, multiple execute
201 bind2maps emacs viins -- -s "$key_info[Control]h" insert_head
202 #k# Append less, multiple execute
203 bind2maps emacs viins -- -s "$key_info[Control]f" insert_less
204 bind2maps emacs viins vicmd -- -s \' magic-single-quotes
205 bind2maps emacs viins vicmd -- -s \" magic-double-quotes
206 bind2maps emacs viins vicmd -- -s \) magic-parentheses
207 bind2maps emacs viins vicmd -- -s \] magic-square-brackets
208 bind2maps emacs viins vicmd -- -s \} magic-curly-brackets
209 bind2maps emacs viins vicmd -- -s \> magic-angle-brackets
210 fi