3

Different methods exist to show if you're in insert or normal when using VI key binding in ZSH.

Gnome-terminal also allows you to change the caret from block to ibeam | with a call to gconftool-2.

How can I combine these two so that I have an ibeam in insert mode and a block in normal mode?

2 Answers2

4

In recent versions of gnome-terminal, you can use the following escape sequences to change the cursor:

  • \e[0 q or \e[ q: reset to whatever's defined in the profile settings
  • \e[1 q: blinking block
  • \e[2 q: steady block
  • \e[3 q: blinking underline
  • \e[4 q: steady underline
  • \e[5 q: blinking I-beam
  • \e[6 q: steady I-beam

You can print these by e.g. echo -ne '\e[5 q'. Hook these up to your zsh configuration as shown in mpy's answer.

egmont
  • 2,566
  • 1
  • 15
  • 18
3

Putting both answers provided in the question [1, 2] together, I end up with this:

function zle-line-init zle-keymap-select {
    if [[ $KEYMAP == vicmd ]]; then
       gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape block
    elif [[ $KEYMAP == (main|viins) ]]; then
       gconftool-2 --type string --set /apps/gnome-terminal/profiles/Default/cursor_shape ibeam
    fi
}
zle -N zle-line-init
zle -N zle-keymap-select

The difference is that I check the $KEYMAP parameter directly with an if..fi clause and execute the gconftool-2 command instead of altering the prompt.

mpy
  • 28,816