42

I use bindkey -v (for bash-ers set -o vi I think that works in zsh too) or vi(m) mode. but it bugs me that I don't have any visual cue to tell me whether I'm in insert mode or command mode. Does anyone know how I can make my prompt display the mode?

10 Answers 10

23

I found this via SU. Here's the basic example, though I'm still customizing it for myself:

function zle-line-init zle-keymap-select {
    RPS1="${${KEYMAP/vicmd/-- NORMAL --}/(main|viins)/-- INSERT --}"
    RPS2=$RPS1
    zle reset-prompt
}

zle -N zle-line-init
zle -N zle-keymap-select

I'd explain it except I don't really understand it yet

  • 1
    I tried this approach but found one issue. If you do something like CTRL+C while in vi-command mode, the prompt will reset, but indicate youre in command mode when youre really in insert mode. zle-line-init should always change the indicator to insert mode. For some reason $KEYMAP is not updated properly when zle-line-init is called. – Patrick Apr 21 '12 at 19:25
  • 2
    zle reset-prompt will delete 1 (or more) lines above the prompt (if your prompt is multiline) when redrawing :( This is a showstopper for me. – Paweł Gościcki Sep 22 '12 at 19:59
  • @PawełGościcki it seems to be an issue when you have two or more lines of PS1. – Metaphox Mar 8 '13 at 15:14
  • @Metaphox I know, that why I've said "(if your prompt is multiline)". Any fix for that? – Paweł Gościcki Mar 8 '13 at 15:53
  • @PawełGościcki aww sorry i somehow skipped the words in parentheses , bad habit. No, I didn't find a fix for that. What platform are you on? Was wondering if this is OS X specific. – Metaphox Mar 21 '13 at 8:11
18

You've already found zle-keymap-select which is executed whenever the mode changes. You could use it to set some other visual indicator than the prompt, depending on what your terminal supports it (and your taste in mode indicator display, of course).

There is a standard terminfo capability to change the shape of the cursor. However some terminals display the same cursor in both modes. Xterm's notion of a less visible cursor is to make it blink (and this must be enabled with the -bc command line argument or cursorBlink resource).

zle-keymap-select () {
  case $KEYMAP in
    vicmd) print -rn -- $terminfo[cvvis];; # block cursor
    viins|main) print -rn -- $terminfo[cnorm];; # less visible cursor
  esac
}

With some terminals, you can also change the cursor color with print -n '\e]12;pink\a' (by color name) or print -n '\e]12;#abcdef\a' (by RGB specification). These sequences are described in the xterm documentation, in the ctlseqs file; modern terminal emulators typically emulate xterm, though they might not support all its features.

  • For some reason I get main for KEYMAP instead of viins, not sure why. – Graeme Apr 15 '14 at 1:07
  • 1
    @Graeme main is an alias for viins or emacs depending on whether zsh thought your favorite editor was vi or not when it started. I thought it would use viins when switching the mode back from vicmd, but it seems that it uses main instead. Updated. – Gilles Apr 15 '14 at 1:16
  • 1
    You still need to double up with zle-line-init (or whatever alternative) since zle-keymap-select does not get called if hitting enter changes the keymap. – Graeme Apr 15 '14 at 1:28
8

For the people having problems using reset-prompt with multiline prompts, this worked for me: http://zeitlens.com/posts/2014-06-29-howto-zsh-vi-style.html In combination with https://stackoverflow.com/questions/3622943/zsh-vi-mode-status-line I ended up doing:

terminfo_down_sc=$terminfo[cud1]$terminfo[cuu1]$terminfo[sc]$terminfo[cud1]

function insert-mode () { echo "-- INSERT --" }
function normal-mode () { echo "-- NORMAL --" }

precmd () {
    # yes, I actually like to have a new line, then some stuff and then 
    # the input line
    print -rP "
[%D{%a, %d %b %Y, %H:%M:%S}] %n %{$fg[blue]%}%m%{$reset_color%}"

    # this is required for initial prompt and a problem I had with Ctrl+C or
    # Enter when in normal mode (a new line would come up in insert mode,
    # but normal mode would be indicated)
    PS1="%{$terminfo_down_sc$(insert-mode)$terminfo[rc]%}%~ $ "
}
function set-prompt () {
    case ${KEYMAP} in
      (vicmd)      VI_MODE="$(normal-mode)" ;;
      (main|viins) VI_MODE="$(insert-mode)" ;;
      (*)          VI_MODE="$(insert-mode)" ;;
    esac
    PS1="%{$terminfo_down_sc$VI_MODE$terminfo[rc]%}%~ $ "
}

function zle-line-init zle-keymap-select {
    set-prompt
    zle reset-prompt
}
preexec () { print -rn -- $terminfo[el]; }

zle -N zle-line-init
zle -N zle-keymap-select
5

you can try VimMode

  • 4
    I was hoping for something a bit less linkish and a bit more explanatory. I like to know how things work. – xenoterracide Aug 19 '10 at 6:29
  • 1
    Actually it is all there. Look at the comments the functions and how they are bound to the mode change events. – Martin Dec 30 '11 at 18:38
5

This is what I use to change the cursor between 'Block' and 'Beam' shape in zsh:

(Tested with Termite, gnome-terminal and mate-terminal)

# vim mode config
# ---------------

# Activate vim mode.
bindkey -v

# Remove mode switching delay.
KEYTIMEOUT=5

# Change cursor shape for different vi modes.
function zle-keymap-select {
  if [[ ${KEYMAP} == vicmd ]] ||
     [[ $1 = 'block' ]]; then
    echo -ne '\e[1 q'

  elif [[ ${KEYMAP} == main ]] ||
       [[ ${KEYMAP} == viins ]] ||
       [[ ${KEYMAP} = '' ]] ||
       [[ $1 = 'beam' ]]; then
    echo -ne '\e[5 q'
  fi
}
zle -N zle-keymap-select

# Use beam shape cursor on startup.
echo -ne '\e[5 q'

# Use beam shape cursor for each new prompt.
preexec() {
   echo -ne '\e[5 q'
}
  • This will only work on terminals and terminal emulators that understand DECSCUSR. – JdeBP Jun 13 '18 at 16:06
  • 1
    I like this. I worry that writing to preexec might interact with other usages, so I modified it slightly to use add-zsh-hook like so: gist.github.com/MatrixManAtYrService/… – MatrixManAtYrService Oct 13 '18 at 17:19
4

Another solution for changing the cursor shape between I-beam and block (for underscore, use \033[4 q). Add this to your ~/.zshrc.

zle-keymap-select () {
if [ $KEYMAP = vicmd ]; then
    printf "\033[2 q"
else
    printf "\033[6 q"
fi
}
zle -N zle-keymap-select
zle-line-init () {
zle -K viins
printf "\033[6 q"
}
zle -N zle-line-init
bindkey -v

Modified from https://bbs.archlinux.org/viewtopic.php?id=95078. Tested in gnome-terminal 3.22.


Update

Yet another solution to changing the cursor shapes can be found here. This one apparently works for iTerm2, which I don't have the means to test, but adding it in here in case it is useful for someone else. The final addition to your ~/.zshrc would be

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
  • 1
    I confirmed that the updated script for iTerm2 indeed worked. – Jason Denney May 16 '18 at 20:29
  • The first script will only work on terminals and terminal emulators that understand DECSCUSR. – JdeBP Jun 13 '18 at 16:07
  • This is a really elegant solution that doesn't clutter up my shell – tsturzl Oct 10 '18 at 16:25
3

I'm currently using Zsh with Bullet Train theme. Following the example given by Sebastian Blask's answer, I ended up with the code bellow

bindkey -v
KEYTIMEOUT=1

function zle-line-init zle-keymap-select {
    case ${KEYMAP} in
        (vicmd)      BULLETTRAIN_PROMPT_CHAR="N" ;;
        (main|viins) BULLETTRAIN_PROMPT_CHAR="I" ;;
        (*)          BULLETTRAIN_PROMPT_CHAR="I" ;;
    esac
    zle reset-prompt
}

zle -N zle-line-init
zle -N zle-keymap-select

This will only change de default $ to the letters N to normal mode and I to insert mode.

This image is an example when in normal mode I press Ctrl+C:

enter image description here

2

Here's yet another version, based from Sebastian Blask's post. This was intended to be as non-intrusive as possible, as all the other solutions I could find used extra lines, status on the right, or added characters.

This simply changes the color of $ from white to red when normal mode is enabled. Edit the prompt to your liking

bindkey -v
function zle-line-init zle-keymap-select {
    case ${KEYMAP} in
        (vicmd)      PROMPT=$'%{\e[0;32m%}%~%{\e[0m%} %{\e[0;31m%}$%{\e[0m%} ' ;;
        (main|viins) PROMPT=$'%{\e[0;32m%}%~%{\e[0m%} $ ' ;;
        (*)          PROMPT=$'%{\e[0;32m%}%~%{\e[0m%} $ ' ;;
    esac
    zle reset-prompt
}

zle -N zle-line-init
zle -N zle-keymap-select
1

A version for oh-my-zsh users

There's a plugin for oh-my-zsh called vi-mode that can be found here:

robbyrussell/oh-my-zsh/plugins/vi-mode

I use Antigen to manage my plugins, so including it was as simple as adding this to my .zshrc:

antigen bundle vi-mode
1

The zsh-vim-mode plugin is able to show an indicator for insert, command, search, replace, and visual modes. It uses the basic technique of other answers to hook in to the various ZLE hooks (zle-keymap-select, zle-isearch-update, etc.). It checks [[ $ZLE_STATE = *overwrite* ]] for replace mode. It checks $REGION_ACTIVE to identify visual mode.

The logic is complicated by some quirks of how ZSH fires events when leaving isearch mode.

Another nice feature of the module is the ability to change the cursor shape and color based on the mode. For example, you can use a vertical bar in INSERT mode and a blinking underline in SEARCH mode.

  • This plugin is great. – jdhao Feb 18 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.