vi-mode.plugin.zsh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Ensures that $terminfo values are valid and updates editor information when
  2. # the keymap changes.
  3. function zle-keymap-select zle-line-init zle-line-finish {
  4. # The terminal must be in application mode when ZLE is active for $terminfo
  5. # values to be valid.
  6. if (( ${+terminfo[smkx]} )); then
  7. printf '%s' ${terminfo[smkx]}
  8. fi
  9. if (( ${+terminfo[rmkx]} )); then
  10. printf '%s' ${terminfo[rmkx]}
  11. fi
  12. zle reset-prompt
  13. zle -R
  14. }
  15. # Ensure that the prompt is redrawn when the terminal size changes.
  16. TRAPWINCH() {
  17. if [[ -o zle ]]; then
  18. zle reset-prompt
  19. zle -R
  20. fi
  21. }
  22. zle -N zle-line-init
  23. zle -N zle-line-finish
  24. zle -N zle-keymap-select
  25. zle -N edit-command-line
  26. bindkey -v
  27. # allow v to edit the command line (standard behaviour)
  28. autoload -Uz edit-command-line
  29. bindkey -M vicmd 'v' edit-command-line
  30. # if mode indicator wasn't setup by theme, define default
  31. if [[ "$MODE_INDICATOR" == "" ]]; then
  32. MODE_INDICATOR="%{$fg_bold[red]%}<%{$fg[red]%}<<%{$reset_color%}"
  33. fi
  34. function vi_mode_prompt_info() {
  35. echo "${${KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/}"
  36. }
  37. # define right prompt, if it wasn't defined by a theme
  38. if [[ "$RPS1" == "" && "$RPROMPT" == "" ]]; then
  39. RPS1='$(vi_mode_prompt_info)'
  40. fi