Vim – how to avoid hjkl confusion in Insert mode

 

 

Are you a Vim user? Do you like to use the hjkl keys to move the cursor in Normal mode, like me? If you do, perhaps you’ve experienced the annoyance of forgetting that you are in Insert mode, only to find something like hhhhh or kkkkk appearing on the screen instead of the expected cursor movement. I have to admit that this still happens to me, even after many years. How can it be avoided?

Vim experts advise that you should stay in Normal mode most of the time; it should be the default. I agree, and I do try to remember to do this.  Another idea, which I’ve tried in the past, is to make key mappings such as Alt+h and Alt+l  to move the cursor in both Insert and Normal modes and disable the defaults. But then you’ve lost the simplicity of the one-key hjkl which was the main reason to go down this route in the first place.

The solution I’ve adopted is based on <https://stackoverflow.com/questions/7614546/vim-cursorline-color-change-in-insert-mode>. Here is the code I’m using in .gvimrc with a dark colour scheme (Murphy). (The last character in the bottom line is a zero.)

set cursorline
autocmd InsertEnter * highlight CursorLine guifg=white guibg=grey25
autocmd InsertLeave * highlight CursorLine guifg=white guibg=red
set guicursor+=n-v-c:blinkonO

This gives a light grey cursor line in Insert mode which stand out on the dark background but not too starkly.

Insert mode

In Normal mode the cursor line becomes red.

Normal mode

I use the default cursor, which is block in Normal mode and 25% in Insert mode. Both are white; I’ve set the Normal mode cursor not to blink.

I find that this set-up acts as an effective reminder of which mode I’m in.

Leave a Reply

Your email address will not be published.