"au CursorHold * checktime" breaks the command-line window

For “autoread” to work, “checktime” has to be called regularly. One neat way I’ve been suggested to do this is by using this autocommand:

au CursorHold * checktime

Any time I move the cursor, checktime is called.

However, the command-line window (:<C-f>) is now completely broken. Any time I move the cursor I get an error message:


Is there a better way to run checktime regularly? Is there a different autocommand that is better suited? Can perhaps a timer be used to call it regularly without the use of an autocommand?

@Hubro here you go… hope it helps
this is a part of my init.vim

so far i didn’t get any errors using this, it ran smoothly
little extra feature below shows which file is changed on disk :slight_smile:

set autoread
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif
autocmd FileChangedShellPost *
  \ echohl WarningMsg | echo "File " . expand('<afile>') . " changed on disk. Buffer reloaded" | echohl None
1 Like

Thanks, but it still raises errors in the command-line window. Have you tried :<C-f>?

EDIT: Ah, I figured it out!

autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if expand("%f") != "[Command Line]" | checktime | endif
1 Like

You can use getcmdwintype() to check if you’re in the command-line window. Although, there are other cases which you want to lookout for. I use this in my dotfiles:

function auto_checktime()
  local bufnr = tonumber(vim.fn.expand "<abuf>")
  local name = vim.api.nvim_buf_get_name(bufnr)
    name == ""
    -- Only check for normal files
    or vim.bo[bufnr].buftype ~= ""
    -- To avoid: E211: File "..." no longer available
    or not vim.fn.filereadable(name)
  -- only check for the current buffer
  vim.cmd(bufnr .. "checktime")
1 Like