]> git.r.bdr.sh - rbdr/dotfiles/blame_incremental - zsh/modules/editor.zsh
Add universal ctags config
[rbdr/dotfiles] / zsh / modules / editor.zsh
... / ...
CommitLineData
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
134 bindkey -M vicmd "?" history-incremental-pattern-search-backward
135 bindkey -M vicmd "/" history-incremental-pattern-search-forward
136else
137 bindkey -M vicmd "?" history-incremental-search-backward
138 bindkey -M vicmd "/" history-incremental-search-forward
139fi
140
141#
142# Emacs and Vi Key Bindings
143#
144
145for keymap in 'emacs' 'viins'; do
146 bindkey -M "$keymap" "$key_info[Home]" beginning-of-line
147 bindkey -M "$keymap" "$key_info[End]" end-of-line
148
149 bindkey -M "$keymap" "$key_info[Insert]" overwrite-mode
150 bindkey -M "$keymap" "$key_info[Delete]" delete-char
151 bindkey -M "$keymap" "$key_info[Backspace]" backward-delete-char
152
153 bindkey -M "$keymap" "$key_info[Left]" backward-char
154 bindkey -M "$keymap" "$key_info[Right]" forward-char
155
156 # Expand history on space.
157 bindkey -M "$keymap" ' ' magic-space
158
159 # Clear screen.
160 bindkey -M "$keymap" "$key_info[Control]L" clear-screen
161
162 # Expand command name to full path.
163 for key in "$key_info[Escape]"{E,e}
164 bindkey -M "$keymap" "$key" expand-cmd-path
165
166 # Duplicate the previous word.
167 for key in "$key_info[Escape]"{M,m}
168 bindkey -M "$keymap" "$key" copy-prev-shell-word
169
170 # Use a more flexible push-line.
171 for key in "$key_info[Control]Q" "$key_info[Escape]"{q,Q}
172 bindkey -M "$keymap" "$key" push-line-or-edit
173
174 # Bind Shift + Tab to go to the previous menu item.
175 bindkey -M "$keymap" "$key_info[BackTab]" reverse-menu-complete
176
177 # Complete in the middle of word.
178 bindkey -M "$keymap" "$key_info[Control]I" expand-or-complete
179
180 # Display an indicator when completing.
181 bindkey -M "$keymap" "$key_info[Control]I" \
182 expand-or-complete-with-indicator
183
184 # Insert 'sudo ' at the beginning of the line.
185 bindkey -M "$keymap" "$key_info[Control]X$key_info[Control]S" prepend-sudo
186done
187
188#
189# Layout
190#
191
192# Set the key layout.
193bindkey -v
194
195unset key{,map,bindings}