9

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?

0

2 Answers 2

5

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.

1
3

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

augroup auto_checktime
  autocmd!
  " 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 that you have read and understand our privacy policy and code of conduct.

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