vi-mode.plugin.zsh 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. zle && { zle reset-prompt; zle -R }
  18. }
  19. zle -N zle-line-init
  20. zle -N zle-line-finish
  21. zle -N zle-keymap-select
  22. zle -N edit-command-line
  23. bindkey -v
  24. # allow v to edit the command line (standard behaviour)
  25. autoload -Uz edit-command-line
  26. bindkey -M vicmd 'v' edit-command-line
  27. # allow ctrl-p, ctrl-n for navigate history (standard behaviour)
  28. bindkey '^P' up-history
  29. bindkey '^N' down-history
  30. # allow ctrl-h, ctrl-w, ctrl-? for char and word deletion (standard behaviour)
  31. bindkey '^?' backward-delete-char
  32. bindkey '^h' backward-delete-char
  33. bindkey '^w' backward-kill-word
  34. # if mode indicator wasn't setup by theme, define default
  35. if [[ "$MODE_INDICATOR" == "" ]]; then
  36. MODE_INDICATOR="%{$fg_bold[red]%}<%{$fg[red]%}<<%{$reset_color%}"
  37. fi
  38. function vi_mode_prompt_info() {
  39. echo "${${KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/}"
  40. }
  41. # define right prompt, if it wasn't defined by a theme
  42. if [[ "$RPS1" == "" && "$RPROMPT" == "" ]]; then
  43. RPS1='$(vi_mode_prompt_info)'
  44. fi