Sample Header Ad - 728x90

Changing cursor style based on mode in both zsh and vim

21 votes
7 answers
29057 views
I use *vi-mode * in *oh-my-zsh * with the *af-magic theme *. I want the cursor style to indicate whether I am in *normal mode* (block) or *insert mode* (beam), both in zsh and in vim. This is what I have so far: In my ~/.zshrc: # 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 '\e5 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' } As found [here . In vim, I use *Vundle * and *terminus *. With these configurations, both zsh and vim work as they should when considered independently. However, when I enter vim from zsh in *insert mode*, vim starts in *normal mode* (as it should) but still shows the beam shape cursor. Similarly, when I exit vim, I get back to zsh in *insert mode*, but the cursor is still in block shape (since the last mode in vim was *normal*). When after this, I switch modes for the first time (in both zsh and vim), the cursor behaves the way it should again. How can I make them display the correct cursor after entering and exiting vim as well? I tried putting autocmd VimEnter * stopinsert autocmd VimLeave * startinsert in my ~.vimrc, but this does not affect the cursor.
Asked by maddingl (686 rep)
Mar 24, 2018, 03:15 PM
Last activity: Jan 3, 2025, 01:08 AM