vi-mode.plugin.zsh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Updates editor information when the keymap changes.
  2. function zle-keymap-select() {
  3. zle reset-prompt
  4. zle -R
  5. }
  6. # Ensures that MODE_INDITCATOR is displayed on terminal start up.
  7. function zle-line-init() {
  8. zle reset-prompt
  9. }
  10. # Ensure that the prompt is redrawn when the terminal size changes.
  11. TRAPWINCH() {
  12. zle && { zle -R; zle reset-prompt }
  13. }
  14. zle -N zle-keymap-select
  15. zle -N edit-command-line
  16. bindkey -v
  17. # allow v to edit the command line (standard behaviour)
  18. autoload -Uz edit-command-line
  19. bindkey -M vicmd 'v' edit-command-line
  20. # allow ctrl-p, ctrl-n for navigate history (standard behaviour)
  21. bindkey '^P' up-history
  22. bindkey '^N' down-history
  23. # allow ctrl-h, ctrl-w, ctrl-? for char and word deletion (standard behaviour)
  24. bindkey '^?' backward-delete-char
  25. bindkey '^h' backward-delete-char
  26. bindkey '^w' backward-kill-word
  27. # allow ctrl-r to perform backward search in history
  28. bindkey '^r' history-incremental-search-backward
  29. # allow ctrl-a and ctrl-e to move to beginning/end of line
  30. bindkey '^a' beginning-of-line
  31. bindkey '^e' end-of-line
  32. # if mode indicator wasn't setup by theme, define default
  33. if [[ "$MODE_INDICATOR" == "" ]]; then
  34. MODE_INDICATOR="%{$fg_bold[red]%}<%{$fg[red]%}<<%{$reset_color%}"
  35. fi
  36. function vi_mode_prompt_info() {
  37. echo "${${KEYMAP/vicmd/$MODE_INDICATOR}/(main|viins)/}"
  38. }
  39. # define right prompt, if it wasn't defined by a theme
  40. if [[ "$RPS1" == "" && "$RPROMPT" == "" ]]; then
  41. RPS1='$(vi_mode_prompt_info)'
  42. fi