key-bindings.zsh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Relevant manpages:
  2. # - zshzle - binding, widgets
  3. # - zshbuiltins - autoload
  4. # - zshmodules - echoti, $terminfo
  5. # - terminfo - "smkx" and "rmkx"
  6. # Use vi-bindings in ZLE (Zsh Line Editor) for command editing.
  7. bindkey -v
  8. # <S-Tab> to cycle backwards through autocomplete suggestions.
  9. bindkey '^[[Z' reverse-menu-complete
  10. # Command line editing with text editor: `v` in vi-command mode or `<C-x><C-e>`.
  11. #
  12. # Load a function from an fpath folder. e.g. /usr/share/zsh/*/functions.
  13. autoload -Uz edit-command-line
  14. zle -N edit-command-line
  15. # `-N` creates a New user-defined widget that fires a function. In this case,
  16. # since no function is given, the widget will fire a function of the same name.
  17. bindkey '\C-x\C-e' edit-command-line # Bind the widget to keys.
  18. bindkey -M vicmd v edit-command-line # Same, but only for the `vicmd` keymap.
  19. # Make Ctrl-z also bring jobs back to the foreground.
  20. fancy-ctrl-z () {
  21. # Push current input into an input stack. Gets popped when ZLE next opens
  22. # e.g. at next <C-z>, or when foreground jobs finishes.
  23. [[ $#BUFFER -ne 0 ]] && zle push-input
  24. # Bring last job back to foreground
  25. BUFFER="fg"
  26. zle accept-line
  27. }
  28. zle -N fancy-ctrl-z
  29. bindkey '^Z' fancy-ctrl-z
  30. # Key bindings for history search
  31. #
  32. # Start typing + [Up-Arrow] - fuzzy find history forward
  33. if [[ "${terminfo[kcuu1]}" != "" ]]; then
  34. bindkey "${terminfo[kcuu1]}" up-line-or-search
  35. fi
  36. # Start typing + [Down-Arrow] - fuzzy find history backward
  37. if [[ "${terminfo[kcud1]}" != "" ]]; then
  38. bindkey "${terminfo[kcud1]}" down-line-or-search
  39. fi
  40. # [Ctrl-r] - Search backward incrementally for a specified string.
  41. bindkey '^r' history-incremental-search-backward
  42. # If a terminal supports it, make sure to enter `keyboard_transmit` mode when
  43. # zle is active, since only then are values from `$terminfo` valid.
  44. if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
  45. function zle-line-init() {
  46. echoti smkx # Set the terminal into `keyboard_transmit` mode
  47. }
  48. function zle-line-finish() {
  49. echoti rmkx # Leave `keyboard_transmit` mode
  50. }
  51. zle -N zle-line-init # Special Widget: runs before each new line of input.
  52. zle -N zle-line-finish # Special Widget: runs after input has been read.
  53. fi
  54. # Leaving this here because OMZ uses it without explanation, and it took some
  55. # experimentation to find out why. This setting affects what emacs keybindings
  56. # (e.g. M-b and M-f) consider a word. Setting this to an empty string means that
  57. # motions would stop at all non-alphanumeric characters. This is irrelevant for
  58. # vi keybindings which employ the concepts of "words" and "WORDS".
  59. #WORDCHARS=''