key-bindings.zsh 2.5 KB

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