key-bindings.zsh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Relevant manpages:
  2. # - zshzle - binding, widgets
  3. # - zshbuiltins - autoload
  4. # - zshmodules - echoti, $terminfo
  5. # - terminfo - "smkx" and "rmkx"
  6. # Maybe use vi-bindings in ZLE (Zsh Line Editor) for command editing.
  7. [[ $VI_MODE = 1 ]] && bindkey -v || bindkey -e
  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. # [Up & Down Arrow] - search through history for the current input string.
  33. if [[ "${terminfo[kcuu1]}" != "" ]]; then
  34. autoload -U up-line-or-beginning-search
  35. zle -N up-line-or-beginning-search
  36. bindkey "${terminfo[kcuu1]}" up-line-or-beginning-search
  37. fi
  38. if [[ "${terminfo[kcud1]}" != "" ]]; then
  39. autoload -U down-line-or-beginning-search
  40. zle -N down-line-or-beginning-search
  41. bindkey "${terminfo[kcud1]}" down-line-or-beginning-search
  42. fi
  43. # [Ctrl-r] - Search backward incrementally for a specified string.
  44. bindkey '^r' history-incremental-search-backward
  45. # If a terminal supports it, make sure to enter `keyboard_transmit` mode when
  46. # zle is active, since only then are values from `$terminfo` valid.
  47. if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
  48. function zle-line-init() {
  49. echoti smkx # Set the terminal into `keyboard_transmit` mode
  50. }
  51. function zle-line-finish() {
  52. echoti rmkx # Leave `keyboard_transmit` mode
  53. }
  54. zle -N zle-line-init # Special Widget: runs before each new line of input.
  55. zle -N zle-line-finish # Special Widget: runs after input has been read.
  56. fi
  57. # Leaving this here because OMZ uses it without explanation, and it took some
  58. # experimentation to find out why. This setting affects what emacs keybindings
  59. # (e.g. M-b and M-f) consider a word. Setting this to an empty string means that
  60. # motions would stop at all non-alphanumeric characters. This is irrelevant for
  61. # vi keybindings which employ the concepts of "words" and "WORDS".
  62. #WORDCHARS=''