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?
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)
if
name == ""
-- Only check for normal files
or vim.bo[bufnr].buftype ~= ""
-- To avoid: E211: File "..." no longer available
or not vim.fn.filereadable(name)
then
return
end
-- only check for the current buffer
vim.cmd(bufnr .. "checktime")
end