I'm using the answer to this question to create a timeout to insert mode: Alarm for inactivity

Once this timeout has been reached, I want the script to make vim leave insert mode, but I'm not sure what I should use to achieve this?

1 Answer 1


It seems stopinsert worked for me.

The full script would be:

" Exit insert mode after 30 secs with no cursor movement

let g:inactivity_limit = 30  " max Insert mode inactivity before fail, in seconds
let g:check_frequency = 1    " seconds between checks

augroup monitor
    " when vim starts kick off the infinitely repeating calls to the monitor function
    au VimEnter * call timer_start(g:check_frequency * 1000, 'MonitorActivity', {'repeat' : -1})

    " when cursor moves in Insert mode update the last activity time
    au CursorMovedI * let g:last_activity = reltime()
augroup END

func! MonitorActivity(timer_id)
    " when we start we'll initialize the last activity time then return
    " ...gives a little grace period at beginning
    if ! exists('g:last_activity') || empty(g:last_activity)
        let g:last_activity = reltime()

    " very handy function for our purposes, reltime
    let l:diff = reltime(g:last_activity)[0]

    if l:diff > g:inactivity_limit
        " Not exit insert mode and save buffer
        echom "Exiting insert mode after timeout: " . l:diff
        let g:last_activity = []

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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