13 July 2010

change terminal cursor color to reflect vi mode

I just want to write something to start this blog so here is some zshrc config code I wrote.



The code changes the cursor color to red when you go to command mode and back to grey when in insert mode. I assume you are using zsh for your shell and urxvt for your terminal emulator. Add this code to your .zshrc to get it to work:
bindkey -v
zle-keymap-select () {
  if [ $TERM = "rxvt-256color" ]; then
    if [ $KEYMAP = vicmd ]; then
      echo -ne "\033]12;#ff6565\007"
    else
      echo -ne "\033]12;Grey\007"
    fi
  fi
}; zle -N zle-keymap-select
zle-line-init () {
  zle -K viins
  if [ $TERM = "rxvt-245color" ]; then
    echo -ne "\033]12;Grey\007"
  fi
}; zle -N zle-line-init
EDIT: Thanks to mathstuf here is an updated version that works with gnu screen:
bindkey -v
zle-keymap-select () {
  if [ $TERM = "rxvt-256color" ]; then
    if [ $KEYMAP = vicmd ]; then
      echo -ne "\033]12;#ff6565\007"
    else
      echo -ne "\033]12;grey\007"
    fi
  elif [ $TERM = "screen" ]; then
    if [ $KEYMAP = vicmd ]; then
      echo -ne '\033P\033]12;#ff6565\007\033\\'
    else
      echo -ne '\033P\033]12;grey\007\033\\'
    fi
  fi
}; zle -N zle-keymap-select
zle-line-init () {
  zle -K viins
  if [ $TERM = "rxvt-256color" ]; then
    echo -ne "\033]12;grey\007"
  elif [ $TERM = "screen" ]; then
    echo -ne '\033P\033]12;grey\007\033\\'
  fi
}; zle -N zle-line-init
EDIT2: For Patch #262 - 2010/8/30 Thomas E. Dickey has fixed a bug that caused problems with this in xterm.

Here is a version that works with all terminals and fails gracefully in others (as far as I know):
bindkey -v
zle-keymap-select () {
  if [ $TERM = "screen" ]; then
    if [ $KEYMAP = vicmd ]; then
      echo -ne '\033P\033]12;#ff6565\007\033\\'
    else
      echo -ne '\033P\033]12;grey\007\033\\'
    fi
  elif [ $TERM != "linux" ]; then
    if [ $KEYMAP = vicmd ]; then
      echo -ne "\033]12;#ff6565\007"
    else
      echo -ne "\033]12;grey\007"
    fi
  fi
}; zle -N zle-keymap-select
zle-line-init () {
  zle -K viins
  if [ $TERM = "screen" ]; then
    echo -ne '\033P\033]12;grey\007\033\\'
  elif [ $TERM != "linux" ]; then
    echo -ne "\033]12;grey\007"
  fi
}; zle -N zle-line-init
Tagged as config dotfiles linux vi vim zsh zshrc

blog comments powered by Disqus