Modal cursor in zsh in vi mode
This has always been an issue. I use vi mode in zsh (that is the ability to use vi key bindings in the prompt) through activating bindkeys -v in my .zshrc.
But the cursor remains a block regardless of whether you are in insert or normal mode.
In vim this is not a problem, as there is a mode indicator at the bottom of a vim window, if you do not change the cursor shape depending on the mode.
In the terminal, though, it is inconvenient as you have to remember which mode you are in. Many times you are typing a long command and perhaps context-switch to another document to look something up; when you return to the command line and want to resume, what mode are you in?
When I first discovered ZLE and the ability to use vi mode, I immediately looked fora solution to the modal cursor problem. I found one but it was very unstable and would cause weird effects. So I stopped using it.
Today as I was doing some research I found a solution that apparently works in iTerm2 here.
This is the code:
function zle-keymap-select zle-line-init
{
# change cursor shape in iTerm2
case $KEYMAP in
vicmd) print -n -- "\E]50;CursorShape=0\C-G";; # block cursor
viins|main) print -n -- "\E]50;CursorShape=1\C-G";; # line cursor
esac
zle reset-prompt
zle -R
}
function zle-line-finish
{
print -n -- "\E]50;CursorShape=0\C-G" # block cursor
}
zle -N zle-line-init
zle -N zle-line-finish
zle -N zle-keymap-select
Now this weird incantation seems to work.