key-bindings.zsh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 the `edit-command-line` function from an fpath folder.
  13. # e.g. /usr/share/zsh/*/functions
  14. autoload -Uz edit-command-line
  15. zle -N edit-command-line
  16. # `-N` creates a New user-defined widget that fires a given function.
  17. # OR if no function is given, the widget will fire a function of the same name.
  18. bindkey '\C-x\C-e' edit-command-line # Bind the widget to a key.
  19. bindkey -M vicmd v edit-command-line # Same, for a given keymap (vicmd).
  20. # Make Ctrl-z also bring jobs back to the foreground.
  21. fancy-ctrl-z () {
  22. # Push current input into an input stack. Gets popped when ZLE next opens
  23. # e.g. at next <C-z>, or when foreground jobs finishes.
  24. [[ $#BUFFER -ne 0 ]] && zle push-input
  25. # Bring last job back to foreground
  26. BUFFER="fg"
  27. zle accept-line
  28. }
  29. zle -N fancy-ctrl-z
  30. bindkey '^Z' fancy-ctrl-z
  31. # Key bindings for history search
  32. #
  33. # Start typing + [Up-Arrow] - fuzzy find history forward
  34. if [[ "${terminfo[kcuu1]}" != "" ]]; then
  35. bindkey "${terminfo[kcuu1]}" up-line-or-search
  36. fi
  37. # Start typing + [Down-Arrow] - fuzzy find history backward
  38. if [[ "${terminfo[kcud1]}" != "" ]]; then
  39. bindkey "${terminfo[kcud1]}" down-line-or-search
  40. fi
  41. # [Ctrl-r] - Search backward incrementally for a specified string.
  42. bindkey '^r' history-incremental-search-backward
  43. # If a terminal supports it, make sure to enter `keyboard_transmit` mode when
  44. # zle is active, since only then are values from `$terminfo` valid.
  45. if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
  46. function zle-line-init() {
  47. echoti smkx # Set the terminal into `keyboard_transmit` mode
  48. }
  49. function zle-line-finish() {
  50. echoti rmkx # Leave `keyboard_transmit` mode
  51. }
  52. zle -N zle-line-init # Special Widget: runs before each new line of input.
  53. zle -N zle-line-finish # Special Widget: runs after input has been read.
  54. fi