init.zsh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. #
  2. # Sets key bindings.
  3. #
  4. # Authors:
  5. # Sorin Ionescu <sorin.ionescu@gmail.com>
  6. #
  7. # Return if requirements are not found.
  8. if [[ "$TERM" == 'dumb' ]]; then
  9. return 1
  10. fi
  11. #
  12. # Options
  13. #
  14. setopt BEEP # Beep on error in line editor.
  15. #
  16. # Variables
  17. #
  18. # Treat these characters as part of a word.
  19. zstyle -s ':prezto:module:editor' wordchars 'WORDCHARS' \
  20. || WORDCHARS='*?_-.[]~&;!#$%^(){}<>'
  21. # Use human-friendly identifiers.
  22. zmodload zsh/terminfo
  23. typeset -gA key_info
  24. key_info=(
  25. 'Control' '\C-'
  26. 'ControlLeft' '\e[1;5D \e[5D \e\e[D \eOd'
  27. 'ControlRight' '\e[1;5C \e[5C \e\e[C \eOc'
  28. 'ControlPageUp' '\e[5;5~'
  29. 'ControlPageDown' '\e[6;5~'
  30. 'Escape' '\e'
  31. 'Meta' '\M-'
  32. 'Backspace' "^?"
  33. 'Delete' "^[[3~"
  34. 'F1' "$terminfo[kf1]"
  35. 'F2' "$terminfo[kf2]"
  36. 'F3' "$terminfo[kf3]"
  37. 'F4' "$terminfo[kf4]"
  38. 'F5' "$terminfo[kf5]"
  39. 'F6' "$terminfo[kf6]"
  40. 'F7' "$terminfo[kf7]"
  41. 'F8' "$terminfo[kf8]"
  42. 'F9' "$terminfo[kf9]"
  43. 'F10' "$terminfo[kf10]"
  44. 'F11' "$terminfo[kf11]"
  45. 'F12' "$terminfo[kf12]"
  46. 'Insert' "$terminfo[kich1]"
  47. 'Home' "$terminfo[khome]"
  48. 'PageUp' "$terminfo[kpp]"
  49. 'End' "$terminfo[kend]"
  50. 'PageDown' "$terminfo[knp]"
  51. 'Up' "$terminfo[kcuu1]"
  52. 'Left' "$terminfo[kcub1]"
  53. 'Down' "$terminfo[kcud1]"
  54. 'Right' "$terminfo[kcuf1]"
  55. 'BackTab' "$terminfo[kcbt]"
  56. )
  57. # Set empty $key_info values to an invalid UTF-8 sequence to induce silent
  58. # bindkey failure.
  59. for key in "${(k)key_info[@]}"; do
  60. if [[ -z "$key_info[$key]" ]]; then
  61. key_info[$key]='�'
  62. fi
  63. done
  64. #
  65. # External Editor
  66. #
  67. # Allow command line editing in an external editor.
  68. autoload -Uz edit-command-line
  69. zle -N edit-command-line
  70. #
  71. # Functions
  72. #
  73. # Runs bindkey but for all of the keymaps. Running it with no arguments will
  74. # print out the mappings for all of the keymaps.
  75. function bindkey-all {
  76. local keymap=''
  77. for keymap in $(bindkey -l); do
  78. [[ "$#" -eq 0 ]] && printf "#### %s\n" "${keymap}" 1>&2
  79. bindkey -M "${keymap}" "$@"
  80. done
  81. }
  82. # Exposes information about the Zsh Line Editor via the $editor_info associative
  83. # array.
  84. function editor-info {
  85. # Ensure that we're going to set the editor-info for prompts that
  86. # are prezto managed and/or compatible.
  87. if zstyle -t ':prezto:module:prompt' managed; then
  88. # Clean up previous $editor_info.
  89. unset editor_info
  90. typeset -gA editor_info
  91. if [[ "$KEYMAP" == 'vicmd' ]]; then
  92. zstyle -s ':prezto:module:editor:info:keymap:alternate' format 'REPLY'
  93. editor_info[keymap]="$REPLY"
  94. else
  95. zstyle -s ':prezto:module:editor:info:keymap:primary' format 'REPLY'
  96. editor_info[keymap]="$REPLY"
  97. if [[ "$ZLE_STATE" == *overwrite* ]]; then
  98. zstyle -s ':prezto:module:editor:info:keymap:primary:overwrite' format 'REPLY'
  99. editor_info[overwrite]="$REPLY"
  100. else
  101. zstyle -s ':prezto:module:editor:info:keymap:primary:insert' format 'REPLY'
  102. editor_info[overwrite]="$REPLY"
  103. fi
  104. fi
  105. unset REPLY
  106. zle zle-reset-prompt
  107. fi
  108. }
  109. zle -N editor-info
  110. # Reset the prompt based on the current context and whether the prompt utilizes
  111. # the editor:info zstyle. If the prompt does utilize the editor:info, we must
  112. # reset the prompt, otherwise the change in the prompt will never update. If the
  113. # prompt does not utilize the editor:info, we simply redisplay the command line.
  114. function zle-reset-prompt {
  115. # Explicitly check to see if there is an editor info keymap set that would
  116. # require a reset of the prompt
  117. if zstyle -L ':prezto:module:editor:info*' | grep -v 'completing' > /dev/null 2>&1; then
  118. # If we aren't within one of the specified contexts, then we want to reset
  119. # the prompt with the appropriate editor_info[keymap] if there is one.
  120. if [[ $CONTEXT != (select|cont) ]]; then
  121. zle reset-prompt
  122. fi
  123. fi
  124. zle -R
  125. }
  126. zle -N zle-reset-prompt
  127. # Updates editor information when the keymap changes.
  128. function zle-keymap-select {
  129. zle editor-info
  130. }
  131. zle -N zle-keymap-select
  132. # Enables terminal application mode and updates editor information.
  133. function zle-line-init {
  134. # The terminal must be in application mode when ZLE is active for $terminfo
  135. # values to be valid.
  136. if (( $+terminfo[smkx] )); then
  137. # Enable terminal application mode.
  138. echoti smkx
  139. fi
  140. # Update editor information.
  141. zle editor-info
  142. }
  143. zle -N zle-line-init
  144. # Disables terminal application mode and updates editor information.
  145. function zle-line-finish {
  146. # The terminal must be in application mode when ZLE is active for $terminfo
  147. # values to be valid.
  148. if (( $+terminfo[rmkx] )); then
  149. # Disable terminal application mode.
  150. echoti rmkx
  151. fi
  152. # Update editor information.
  153. zle editor-info
  154. }
  155. zle -N zle-line-finish
  156. # Toggles emacs overwrite mode and updates editor information.
  157. function overwrite-mode {
  158. zle .overwrite-mode
  159. zle editor-info
  160. }
  161. zle -N overwrite-mode
  162. # Enters vi insert mode and updates editor information.
  163. function vi-insert {
  164. zle .vi-insert
  165. zle editor-info
  166. }
  167. zle -N vi-insert
  168. # Moves to the first non-blank character then enters vi insert mode and updates
  169. # editor information.
  170. function vi-insert-bol {
  171. zle .vi-insert-bol
  172. zle editor-info
  173. }
  174. zle -N vi-insert-bol
  175. # Enters vi replace mode and updates editor information.
  176. function vi-replace {
  177. zle .vi-replace
  178. zle editor-info
  179. }
  180. zle -N vi-replace
  181. # Expands .... to ../..
  182. function expand-dot-to-parent-directory-path {
  183. if [[ $LBUFFER = *.. ]]; then
  184. LBUFFER+='/..'
  185. else
  186. LBUFFER+='.'
  187. fi
  188. }
  189. zle -N expand-dot-to-parent-directory-path
  190. # Displays an indicator when completing.
  191. function expand-or-complete-with-indicator {
  192. local indicator
  193. zstyle -s ':prezto:module:editor:info:completing' format 'indicator'
  194. # This is included to work around a bug in zsh which shows up when interacting
  195. # with multi-line prompts.
  196. if [[ -z "$indicator" ]]; then
  197. zle expand-or-complete
  198. return
  199. fi
  200. print -Pn "$indicator"
  201. zle expand-or-complete
  202. zle redisplay
  203. }
  204. zle -N expand-or-complete-with-indicator
  205. # Inserts 'sudo ' at the beginning of the line.
  206. function prepend-sudo {
  207. if [[ "$BUFFER" != su(do|)\ * ]]; then
  208. BUFFER="sudo $BUFFER"
  209. (( CURSOR += 5 ))
  210. fi
  211. }
  212. zle -N prepend-sudo
  213. # Expand aliases
  214. function glob-alias {
  215. zle _expand_alias
  216. zle expand-word
  217. zle magic-space
  218. }
  219. zle -N glob-alias
  220. # Toggle the comment character at the start of the line. This is meant to work
  221. # around a buggy implementation of pound-insert in zsh.
  222. #
  223. # This is currently only used for the emacs keys because vi-pound-insert has
  224. # been reported to work properly.
  225. function pound-toggle {
  226. if [[ "$BUFFER" = '#'* ]]; then
  227. # Because of an oddity in how zsh handles the cursor when the buffer size
  228. # changes, we need to make this check before we modify the buffer and let
  229. # zsh handle moving the cursor back if it's past the end of the line.
  230. if [[ $CURSOR != $#BUFFER ]]; then
  231. (( CURSOR -= 1 ))
  232. fi
  233. BUFFER="${BUFFER:1}"
  234. else
  235. BUFFER="#$BUFFER"
  236. (( CURSOR += 1 ))
  237. fi
  238. }
  239. zle -N pound-toggle
  240. # Reset to default key bindings.
  241. bindkey -d
  242. #
  243. # Emacs Key Bindings
  244. #
  245. for key in "$key_info[Escape]"{B,b} "${(s: :)key_info[ControlLeft]}" \
  246. "${key_info[Escape]}${key_info[Left]}"
  247. bindkey -M emacs "$key" emacs-backward-word
  248. for key in "$key_info[Escape]"{F,f} "${(s: :)key_info[ControlRight]}" \
  249. "${key_info[Escape]}${key_info[Right]}"
  250. bindkey -M emacs "$key" emacs-forward-word
  251. # Kill to the beginning of the line.
  252. for key in "$key_info[Escape]"{K,k}
  253. bindkey -M emacs "$key" backward-kill-line
  254. # Redo.
  255. bindkey -M emacs "$key_info[Escape]_" redo
  256. # Search previous character.
  257. bindkey -M emacs "$key_info[Control]X$key_info[Control]B" vi-find-prev-char
  258. # Match bracket.
  259. bindkey -M emacs "$key_info[Control]X$key_info[Control]]" vi-match-bracket
  260. # Edit command in an external editor.
  261. bindkey -M emacs "$key_info[Control]X$key_info[Control]E" edit-command-line
  262. if (( $+widgets[history-incremental-pattern-search-backward] )); then
  263. bindkey -M emacs "$key_info[Control]R" \
  264. history-incremental-pattern-search-backward
  265. bindkey -M emacs "$key_info[Control]S" \
  266. history-incremental-pattern-search-forward
  267. fi
  268. # Toggle comment at the start of the line. Note that we use pound-toggle which
  269. # is similar to pount insert, but meant to work around some issues that were
  270. # being seen in iTerm.
  271. bindkey -M emacs "$key_info[Escape];" pound-toggle
  272. #
  273. # Vi Key Bindings
  274. #
  275. # Edit command in an external editor emacs style (v is used for visual mode)
  276. bindkey -M vicmd "$key_info[Control]X$key_info[Control]E" edit-command-line
  277. # Undo/Redo
  278. bindkey -M vicmd "u" undo
  279. bindkey -M viins "$key_info[Control]_" undo
  280. bindkey -M vicmd "$key_info[Control]R" redo
  281. if (( $+widgets[history-incremental-pattern-search-backward] )); then
  282. bindkey -M vicmd "?" history-incremental-pattern-search-backward
  283. bindkey -M vicmd "/" history-incremental-pattern-search-forward
  284. else
  285. bindkey -M vicmd "?" history-incremental-search-backward
  286. bindkey -M vicmd "/" history-incremental-search-forward
  287. fi
  288. # Toggle comment at the start of the line.
  289. bindkey -M vicmd "#" vi-pound-insert
  290. #
  291. # Emacs and Vi Key Bindings
  292. #
  293. # Unbound keys in vicmd and viins mode will cause really odd things to happen
  294. # such as the casing of all the characters you have typed changing or other
  295. # undefined things. In emacs mode they just insert a tilde, but bind these keys
  296. # in the main keymap to a noop op so if there is no keybind in the users mode
  297. # it will fall back and do nothing.
  298. function _prezto-zle-noop { ; }
  299. zle -N _prezto-zle-noop
  300. local -a unbound_keys
  301. unbound_keys=(
  302. "${key_info[F1]}"
  303. "${key_info[F2]}"
  304. "${key_info[F3]}"
  305. "${key_info[F4]}"
  306. "${key_info[F5]}"
  307. "${key_info[F6]}"
  308. "${key_info[F7]}"
  309. "${key_info[F8]}"
  310. "${key_info[F9]}"
  311. "${key_info[F10]}"
  312. "${key_info[F11]}"
  313. "${key_info[F12]}"
  314. "${key_info[PageUp]}"
  315. "${key_info[PageDown]}"
  316. "${key_info[ControlPageUp]}"
  317. "${key_info[ControlPageDown]}"
  318. )
  319. for keymap in $unbound_keys; do
  320. bindkey -M viins "${keymap}" _prezto-zle-noop
  321. bindkey -M vicmd "${keymap}" _prezto-zle-noop
  322. done
  323. # Keybinds for all keymaps
  324. for keymap in 'emacs' 'viins' 'vicmd'; do
  325. bindkey -M "$keymap" "$key_info[Home]" beginning-of-line
  326. bindkey -M "$keymap" "$key_info[End]" end-of-line
  327. done
  328. # Keybinds for all vi keymaps
  329. for keymap in viins vicmd; do
  330. # Ctrl + Left and Ctrl + Right bindings to forward/backward word
  331. for key in "${(s: :)key_info[ControlLeft]}"
  332. bindkey -M "$keymap" "$key" vi-backward-word
  333. for key in "${(s: :)key_info[ControlRight]}"
  334. bindkey -M "$keymap" "$key" vi-forward-word
  335. done
  336. # Keybinds for emacs and vi insert mode
  337. for keymap in 'emacs' 'viins'; do
  338. bindkey -M "$keymap" "$key_info[Insert]" overwrite-mode
  339. bindkey -M "$keymap" "$key_info[Delete]" delete-char
  340. bindkey -M "$keymap" "$key_info[Backspace]" backward-delete-char
  341. bindkey -M "$keymap" "$key_info[Left]" backward-char
  342. bindkey -M "$keymap" "$key_info[Right]" forward-char
  343. # Expand history on space.
  344. bindkey -M "$keymap" ' ' magic-space
  345. # Clear screen.
  346. bindkey -M "$keymap" "$key_info[Control]L" clear-screen
  347. # Expand command name to full path.
  348. for key in "$key_info[Escape]"{E,e}
  349. bindkey -M "$keymap" "$key" expand-cmd-path
  350. # Duplicate the previous word.
  351. for key in "$key_info[Escape]"{M,m}
  352. bindkey -M "$keymap" "$key" copy-prev-shell-word
  353. # Use a more flexible push-line.
  354. for key in "$key_info[Control]Q" "$key_info[Escape]"{q,Q}
  355. bindkey -M "$keymap" "$key" push-line-or-edit
  356. # Bind Shift + Tab to go to the previous menu item.
  357. bindkey -M "$keymap" "$key_info[BackTab]" reverse-menu-complete
  358. # Complete in the middle of word.
  359. bindkey -M "$keymap" "$key_info[Control]I" expand-or-complete
  360. # Expand .... to ../..
  361. if zstyle -t ':prezto:module:editor' dot-expansion; then
  362. bindkey -M "$keymap" "." expand-dot-to-parent-directory-path
  363. fi
  364. # Display an indicator when completing.
  365. bindkey -M "$keymap" "$key_info[Control]I" \
  366. expand-or-complete-with-indicator
  367. # Insert 'sudo ' at the beginning of the line.
  368. bindkey -M "$keymap" "$key_info[Control]X$key_info[Control]S" prepend-sudo
  369. # control-space expands all aliases, including global
  370. bindkey -M "$keymap" "$key_info[Control] " glob-alias
  371. done
  372. # Delete key deletes character in vimcmd cmd mode instead of weird default functionality
  373. bindkey -M vicmd "$key_info[Delete]" delete-char
  374. # Do not expand .... to ../.. during incremental search.
  375. if zstyle -t ':prezto:module:editor' dot-expansion; then
  376. bindkey -M isearch . self-insert 2> /dev/null
  377. fi
  378. #
  379. # Layout
  380. #
  381. # Set the key layout.
  382. zstyle -s ':prezto:module:editor' key-bindings 'key_bindings'
  383. if [[ "$key_bindings" == (emacs|) ]]; then
  384. bindkey -e
  385. elif [[ "$key_bindings" == vi ]]; then
  386. bindkey -v
  387. else
  388. print "prezto: editor: invalid key bindings: $key_bindings" >&2
  389. fi
  390. unset key{,map,_bindings}