vi-mode.plugin.zsh 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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] && $+terminfo[rmkx] )); then
  7. case "$0" in
  8. (zle-line-init)
  9. # Enable terminal application mode.
  10. printf '%s' ${terminfo[smkx]}
  11. ;;
  12. (zle-line-finish)
  13. # Disable terminal application mode.
  14. printf '%s' ${terminfo[rmkx]}
  15. ;;
  16. esac
  17. fi
  18. zle reset-prompt
  19. zle -R
  20. }
  21. zle -N zle-line-init
  22. zle -N zle-line-finish
  23. zle -N zle-keymap-select
  24. bindkey -v
  25. # if mode indicator wasn't setup by theme, define default
  26. if [[ "$MODE_INDICATOR" == "" ]]; then
  27. MODE_INDICATOR="%{$fg_bold[red]%}<%{$fg[red]%}<<%{$reset_color%}"
  28. fi
  29. function vi_mode_prompt_info() {
  30. echo "${${KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/}"
  31. }
  32. # define right prompt, if it wasn't defined by a theme
  33. if [[ "$RPS1" == "" && "$RPROMPT" == "" ]]; then
  34. RPS1='$(vi_mode_prompt_info)'
  35. fi