I have the following in my vimrc to automatically reload a buffer:

" Triger `autoread` when files changes on disk
" https://unix.stackexchange.com/a/383044/143394
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * checktime

The problem is if I'm in command-line mode (ie, after typing : in normal mode), and I change focus away from, the back to vim I get:

Error detected while processing FocusGained Auto commands for "*":
E523: Not allowed here: checktime

How can I prevent this autocmd running if I'm in command-line mode at the time?

Is there a flag set when entering commandline mode? Perhaps an autocmd could set one?


2 Answers 2


You can use this:

autocmd! FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif

The mode() function allows you to test the current mode (see :h mode() for a list of the possible returned values), in command mode it will return c.

With this command, you can add a check to disallow the execution of your autocommand when you are in command mode you should be fine.


After checking out the modes in :h mode(), I use:

augroup auto_checktime
  " Notify if file is changed outside of vim
  " Trigger `checktime` when files changes on disk
  " https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044
  " https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode
  autocmd FocusGained,BufEnter,CursorHold,CursorHoldI *
          \ if mode() !~ '\v(c|r.?|!|t)' && getcmdwintype() == '' | checktime | endif
augroup END

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.