key-bindings.zsh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Overridden keybindings to have up arrow history matching and vi-mode.
  2. #
  3. # vi-mode plugin and oh-my-zsh's key-binding library don't match well, because
  4. # of the way `bindkey -v` overrides previously made bindings.
  5. #
  6. # Unfortunately, there is no nice way to reconcile the two, so parts of both
  7. # have been extracted and placed here.
  8. # Make sure that the terminal is in application mode when zle is active, since
  9. # only then values from $terminfo are valid
  10. if (( ${+terminfo[smkx]} )) && (( ${+terminfo[rmkx]} )); then
  11. function zle-line-init() {
  12. echoti smkx
  13. }
  14. function zle-line-finish() {
  15. echoti rmkx
  16. }
  17. zle -N zle-line-init
  18. zle -N zle-line-finish
  19. fi
  20. # From vi-mode: needed to get prompt indicator to work in `cinaeco.zsh-theme`
  21. function zle-keymap-select {
  22. # The terminal must be in application mode when ZLE is active for $terminfo
  23. # values to be valid.
  24. if (( ${+terminfo[smkx]} )); then
  25. printf '%s' ${terminfo[smkx]}
  26. fi
  27. if (( ${+terminfo[rmkx]} )); then
  28. printf '%s' ${terminfo[rmkx]}
  29. fi
  30. zle reset-prompt
  31. zle -R
  32. }
  33. zle -N zle-keymap-select
  34. # Start with vim bindings
  35. bindkey -v
  36. # Key bindings for history search
  37. bindkey '^r' history-incremental-search-backward # [Ctrl-r] - Search backward incrementally for a specified string. The string may begin with ^ to anchor the search to the beginning of the line.
  38. if [[ "${terminfo[kcuu1]}" != "" ]]; then
  39. bindkey "${terminfo[kcuu1]}" up-line-or-search # start typing + [Up-Arrow] - fuzzy find history forward
  40. fi
  41. if [[ "${terminfo[kcud1]}" != "" ]]; then
  42. bindkey "${terminfo[kcud1]}" down-line-or-search # start typing + [Down-Arrow] - fuzzy find history backward
  43. fi
  44. # Ctrl-z for `fg`
  45. # From http://sheerun.net/2014/03/21/how-to-boost-your-vim-productivity/
  46. fancy-ctrl-z () {
  47. if [[ $#BUFFER -eq 0 ]]; then
  48. BUFFER="fg"
  49. zle accept-line
  50. else
  51. zle push-input
  52. zle clear-screen
  53. fi
  54. }
  55. zle -N fancy-ctrl-z
  56. bindkey '^Z' fancy-ctrl-z
  57. # <S-Tab> to tab backwards through autocomplete suggestions.
  58. bindkey '^[[Z' reverse-menu-complete
  59. # Toggle Command Line Editing with <C-x><C-e>, like bash.
  60. autoload -U edit-command-line
  61. zle -N edit-command-line
  62. bindkey '\C-x\C-e' edit-command-line