Here is my take on VI mode indicator in zsh’s prompt. This is useful only for people who use the vi mode (bindkey -v) in ZSH.
vim_ins_mode="%{$fg[cyan]%}[INS]%{$reset_color%}"
vim_cmd_mode="%{$fg[green]%}[CMD]%{$reset_color%}"
vim_mode=$vim_ins_mode
function zle-keymap-select {
vim_mode="${${KEYMAP/vicmd/${vim_cmd_mode}}/(main|viins)/${vim_ins_mode}}"
zle reset-prompt
}
zle -N zle-keymap-select
function zle-line-finish {
vim_mode=$vim_ins_mode
}
zle -N zle-line-finish
And then it’s a matter of adding ${vim_mode} somewhere in your prompt. For example like this:
RPROMPT='${vim_mode}'
Other examples on the web use zle reset-prompt in the zle-line-init, which has a very nasty side effect of deleting last couple of lines on mode change (when going from ins to cmd mode) when using multi-line prompt. Using zle-line-finish works around that.
Unfortunately, it still suffers from one issue that so far I was unable to solve. When you press Ctrl+c while in cmd mode, the indicator will say you’re still in the cmd mode, while in fact you are in ins mode.
Perhaps someone will find a workaround for that as well?
The other problem I have with this is that I have the last command return status in my prompt and this gets changed to 0 causing the first line to jump move. Not sure how to avoid that. I’m actually tempted to wipe the left prompt in command mode.
I’m using left prompt with last command return status as well and I cannot experience this issue. There is one situation when prompt jumps 1 line above, deleting what was on the screen, but it’s zsh’s bug and it happens so rarely that I just ignore it.
Thank you! This worked great. I am now close to achieving command prompt perfection.