]> git.r.bdr.sh - rbdr/dotfiles/blob - zsh/modules/editor.zsh
Use shift modifier to create kitty instances
[rbdr/dotfiles] / zsh / modules / editor.zsh
1 #
2 # Sets key bindings.
3 #
4 # Authors:
5 # Sorin Ionescu <sorin.ionescu@gmail.com>
6 #
7
8 # Return if requirements are not found.
9 if [[ "$TERM" == 'dumb' ]]; then
10 return 1
11 fi
12
13 #
14 # Options
15 #
16
17 # Beep on error in line editor.
18 setopt BEEP
19
20 #
21 # Variables
22 #
23
24 # Treat these characters as part of a word.
25 WORDCHARS='*?_-.[]~&;!#$%^(){}<>'
26
27 # Use human-friendly identifiers.
28 zmodload zsh/terminfo
29 typeset -gA key_info
30 key_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.
64 for key in "${(k)key_info[@]}"; do
65 if [[ -z "$key_info[$key]" ]]; then
66 key_info[$key]='�'
67 fi
68 done
69
70 #
71 # External Editor
72 #
73
74 # Allow command line editing in an external editor.
75 autoload -Uz edit-command-line
76 zle -N edit-command-line
77
78 # Enables terminal application mode and updates editor information.
79 function 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 }
87 zle -N zle-line-init
88
89 # Disables terminal application mode and updates editor information.
90 function 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 }
98 zle -N zle-line-finish
99
100 # Displays an indicator when completing.
101 function 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 }
108 zle -N expand-or-complete-with-indicator
109
110 # Inserts 'sudo ' at the beginning of the line.
111 function prepend-sudo {
112 if [[ "$BUFFER" != su(do|)\ * ]]; then
113 BUFFER="sudo $BUFFER"
114 (( CURSOR += 5 ))
115 fi
116 }
117 zle -N prepend-sudo
118
119 # Reset to default key bindings.
120 bindkey -d
121
122 #
123 # Vi Key Bindings
124 #
125
126 # Edit command in an external editor.
127 bindkey -M vicmd "v" edit-command-line
128
129 # Undo/Redo
130 bindkey -M vicmd "u" undo
131 bindkey -M vicmd "$key_info[Control]R" redo
132
133 if (( $+widgets[history-incremental-pattern-search-backward] )); then
134 bindkey '^R' history-incremental-pattern-search-backward
135 bindkey -M vicmd "?" history-incremental-pattern-search-backward
136 bindkey -M vicmd "/" history-incremental-pattern-search-forward
137 else
138 bindkey '^R' history-incremental-search-backward
139 bindkey -M vicmd "?" history-incremental-search-backward
140 bindkey -M vicmd "/" history-incremental-search-forward
141 fi
142
143 #
144 # Emacs and Vi Key Bindings
145 #
146
147 for 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
188 done
189
190 #
191 # Layout
192 #
193
194 # Set the key layout.
195 bindkey -v
196
197 unset key{,map,bindings}