user3876493
user3876493

Reputation: 41

Lag in InsertLeave of Vim

I have these lines in my .vimrc to change the color of Statusline for Vim's Insert Mode. The StatusLine responds quickly for InsertEnter. However, for InsertLeave, there is approx. 1 second delay between the disappearance of --INSERT-- and the change of color in StatusLine. May I please get some help with this?

set laststatus=2
if version >= 700
  au InsertEnter * hi StatusLine term=reverse ctermbg=15 ctermfg=22
  au InsertLeave * hi StatusLine term=reverse ctermbg=16 ctermfg=0
endif

I tried :au Insertleave and only shows one command:

--- Auto-Commands ---
InsertLeave
    *         hi StatusLine term=reverse ctermbg=16 ctermfg=0

Any help is appreciated.

Thanks

Upvotes: 2

Views: 884

Answers (2)

Denis
Denis

Reputation: 383

You can use timer_start function with current vim mode check to execute your command only when Escape key pressed and not inside sequences (arrow movements, key bindings and so on).

function <SID>condInsertLeave()
    if mode() == "n"
        hi StatusLine term=reverse ctermbg=16 ctermfg=0
    endif
endfunction

autocmd InsertLeave * call timer_start(200, { tid -> <SID>condInsertLeave()})

Upvotes: 1

FDinoff
FDinoff

Reputation: 31419

Vim can't tell that you are leaving insert mode because all it has seen is an escape. Arrow keys are generally set to interpreted by terminal vim as <ESC>OA, <ESC>OB, <ESC>OC, and <ESC>OD. So vim is waiting for the next key in the sequence before doing anything. This is also the reason if you type <ESC>O the O just sits on the screen for a second instead of opening a new line above the current.

Vim uses timeoutlen to determine how long to wait between key presses. This default to 1000 milliseconds. You can decrease this if you want however it is going to make typing mappings harder.

The autocmd will also be fired faster if you type something immediately afterwords that isn't part of some mapping.

Relevant options to look at are :h timeout, :h ttimeout, :h timeoutlen, and :h ttimeoutlen.

Upvotes: 5

Related Questions