]> git.r.bdr.sh - rbdr/dotfiles/blame - zsh/modules/editor.zsh
Update karabiner
[rbdr/dotfiles] / zsh / modules / editor.zsh
CommitLineData
fa250235
BB
1#
2# Sets key bindings.
3#
4# Authors:
5# Sorin Ionescu <sorin.ionescu@gmail.com>
6#
7
8# Return if requirements are not found.
9if [[ "$TERM" == 'dumb' ]]; then
10 return 1
11fi
12
13#
14# Options
15#
16
17# Beep on error in line editor.
18setopt BEEP
19
20#
21# Variables
22#
23
24# Treat these characters as part of a word.
25WORDCHARS='*?_-.[]~&;!#$%^(){}<>'
26
27# Use human-friendly identifiers.
28zmodload zsh/terminfo
29typeset -gA key_info
30key_info=(
31 'Control' '\C-'
32 'ControlLeft' '\e[1;5D \e[5D \e\e[D \eOd'
33 'ControlRight' '\e[1;5C \e[5C \e\e[C \eOc'
34 'Escape' '\e'
35 'Meta' '\M-'
36 'Backspace' "^?"
37 'Delete' "^[[3~"
38 'F1' "$terminfo[kf1]"
39 'F2' "$terminfo[kf2]"
40 'F3' "$terminfo[kf3]"
41 'F4' "$terminfo[kf4]"
42 'F5' "$terminfo[kf5]"
43 'F6' "$terminfo[kf6]"
44 'F7' "$terminfo[kf7]"
45 'F8' "$terminfo[kf8]"
46 'F9' "$terminfo[kf9]"
47 'F10' "$terminfo[kf10]"
48 'F11' "$terminfo[kf11]"
49 'F12' "$terminfo[kf12]"
50 'Insert' "$terminfo[kich1]"
51 'Home' "$terminfo[khome]"
52 'PageUp' "$terminfo[kpp]"
53 'End' "$terminfo[kend]"
54 'PageDown' "$terminfo[knp]"
55 'Up' "$terminfo[kcuu1]"
56 'Left' "$terminfo[kcub1]"
57 'Down' "$terminfo[kcud1]"
58 'Right' "$terminfo[kcuf1]"
59 'BackTab' "$terminfo[kcbt]"
60)
61
62# Set empty $key_info values to an invalid UTF-8 sequence to induce silent
63# bindkey failure.
64for key in "${(k)key_info[@]}"; do
65 if [[ -z "$key_info[$key]" ]]; then
66 key_info[$key]='�'
67 fi
68done
69
70#
71# External Editor
72#
73
74# Allow command line editing in an external editor.
75autoload -Uz edit-command-line
76zle -N edit-command-line
77
78# Enables terminal application mode and updates editor information.
79function zle-line-init {
80 # The terminal must be in application mode when ZLE is active for $terminfo
81 # values to be valid.
82 if (( $+terminfo[smkx] )); then
83 # Enable terminal application mode.
84 echoti smkx
85 fi
86}
87zle -N zle-line-init
88
89# Disables terminal application mode and updates editor information.
90function zle-line-finish {
91 # The terminal must be in application mode when ZLE is active for $terminfo
92 # values to be valid.
93 if (( $+terminfo[rmkx] )); then
94 # Disable terminal application mode.
95 echoti rmkx
96 fi
97}
98zle -N zle-line-finish
99
100# Displays an indicator when completing.
101function expand-or-complete-with-indicator {
102 local indicator
103 zstyle -s ':prezto:module:editor:info:completing' format 'indicator'
104 print -Pn "$indicator"
105 zle expand-or-complete
106 zle redisplay
107}
108zle -N expand-or-complete-with-indicator
109
110# Inserts 'sudo ' at the beginning of the line.
111function prepend-sudo {
112 if [[ "$BUFFER" != su(do|)\ * ]]; then
113 BUFFER="sudo $BUFFER"
114 (( CURSOR += 5 ))
115 fi
116}
117zle -N prepend-sudo
118
119# Reset to default key bindings.
120bindkey -d
121
122#
123# Vi Key Bindings
124#
125
126# Edit command in an external editor.
127bindkey -M vicmd "v" edit-command-line
128
129# Undo/Redo
130bindkey -M vicmd "u" undo
131bindkey -M vicmd "$key_info[Control]R" redo
132
133if (( $+widgets[history-incremental-pattern-search-backward] )); then
31df1018 134 bindkey '^R' history-incremental-pattern-search-backward
fa250235
BB
135 bindkey -M vicmd "?" history-incremental-pattern-search-backward
136 bindkey -M vicmd "/" history-incremental-pattern-search-forward
137else
31df1018 138 bindkey '^R' history-incremental-search-backward
fa250235
BB
139 bindkey -M vicmd "?" history-incremental-search-backward
140 bindkey -M vicmd "/" history-incremental-search-forward
141fi
142
143#
144# Emacs and Vi Key Bindings
145#
146
147for keymap in 'emacs' 'viins'; do
148 bindkey -M "$keymap" "$key_info[Home]" beginning-of-line
149 bindkey -M "$keymap" "$key_info[End]" end-of-line
150
151 bindkey -M "$keymap" "$key_info[Insert]" overwrite-mode
152 bindkey -M "$keymap" "$key_info[Delete]" delete-char
153 bindkey -M "$keymap" "$key_info[Backspace]" backward-delete-char
154
155 bindkey -M "$keymap" "$key_info[Left]" backward-char
156 bindkey -M "$keymap" "$key_info[Right]" forward-char
157
158 # Expand history on space.
159 bindkey -M "$keymap" ' ' magic-space
160
161 # Clear screen.
162 bindkey -M "$keymap" "$key_info[Control]L" clear-screen
163
164 # Expand command name to full path.
165 for key in "$key_info[Escape]"{E,e}
166 bindkey -M "$keymap" "$key" expand-cmd-path
167
168 # Duplicate the previous word.
169 for key in "$key_info[Escape]"{M,m}
170 bindkey -M "$keymap" "$key" copy-prev-shell-word
171
172 # Use a more flexible push-line.
173 for key in "$key_info[Control]Q" "$key_info[Escape]"{q,Q}
174 bindkey -M "$keymap" "$key" push-line-or-edit
175
176 # Bind Shift + Tab to go to the previous menu item.
177 bindkey -M "$keymap" "$key_info[BackTab]" reverse-menu-complete
178
179 # Complete in the middle of word.
180 bindkey -M "$keymap" "$key_info[Control]I" expand-or-complete
181
182 # Display an indicator when completing.
183 bindkey -M "$keymap" "$key_info[Control]I" \
184 expand-or-complete-with-indicator
185
186 # Insert 'sudo ' at the beginning of the line.
187 bindkey -M "$keymap" "$key_info[Control]X$key_info[Control]S" prepend-sudo
188done
189
190#
191# Layout
192#
193
194# Set the key layout.
195bindkey -v
196
197unset key{,map,bindings}