5

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?

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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