vi-mode.plugin.zsh 1.5 KB

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