A lua based 'auto refresh buffers when they change on disk' function?

Using 0.7 and looking for a Lua implementation of auto refreshing buffers when the files change on disk (eg after pulling from git).

I had this in my options.lua previously which doesn’t seem to be working:

-- trigger `autoread` when files changes on disk
vim.cmd([[
      autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif
      autocmd FileChangedShellPost *
        \ echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None
]])

Has anyone got something similar (preferably lua) for 0.7 that works?

I think all you need is: set autoread. I’m not sure what the lua equivalent is, maybe vim.opt.autoread = true?

Hi, for the record, there is a current attempt at reimplementing autoread using file system watches from LuaJIT, which started following a GSOC:

3 Likes

Yes, in my experience the autoread option is about as much use as a chocolate fire-guard so a rewrite that makes it work (as I expect, anyway) would be great

I have the following in my settings, works about as well as anything can, I suppose:

-- auto-reload files when modified externally
-- https://unix.stackexchange.com/a/383044
vim.o.autoread = true
vim.api.nvim_create_autocmd({ "BufEnter", "CursorHold", "CursorHoldI", "FocusGained" }, {
  command = "if mode() != 'c' | checktime | endif",
  pattern = { "*" },
})
2 Likes

Tried that and it does work, but when it detects changes it isn’t very graceful, it spawns 1/2 a screen high error message in red. That what it does for you too?

haven’t tried but might be of interest GitHub - rktjmp/fwatch.nvim: fwatch.nvim lets you watch files or directories for changes and then run vim commands or lua functions.

I don’t get any error messages at all, are there any clues in the error message about what is wrong?

Here is the error I get. I open/switch buffers with Telescope so maybe that is the issue?

5108: Error executing lua .../packer/opt/telescope.nvim/lua/telescope/actions/set.lua:81: Vim(append):Error executing lua callback: vim/filetype.lua:0: Vim(append):Error executing lua callback: ...treesitter/lua/nvim-treesitter/incremental_selection.lua:1
43: E31: No such mapping
stack traceback:
        [C]: in function 'nvim_buf_del_keymap'
        ...treesitter/lua/nvim-treesitter/incremental_selection.lua:143: in function 'detach'
        ...er/start/nvim-treesitter/lua/nvim-treesitter/configs.lua:478: in function 'detach_module'
        ...er/start/nvim-treesitter/lua/nvim-treesitter/configs.lua:487: in function 'reattach_module'
        ...er/start/nvim-treesitter/lua/nvim-treesitter/configs.lua:106: in function <...er/start/nvim-treesitter/lua/nvim-treesitter/configs.lua:105>
        [C]: in function 'nvim_buf_set_option'
        vim/filetype.lua: in function ''
        vim/filetype.lua: in function 'match'
        ...ocal/Cellar/neovim/0.7.0/share/nvim/runtime/filetype.lua:15: in function <...ocal/Cellar/neovim/0.7.0/share/nvim/runtime/filetype.lua:14>
        [C]: in function 'cmd'
        .../packer/opt/telescope.nvim/lua/telescope/actions/set.lua:81: in function 'edit_buffer'
        .../packer/opt/telescope.nvim/lua/telescope/actions/set.lua:148: in function 'run_replace_or_original'
        ...k/packer/opt/telescope.nvim/lua/telescope/actions/mt.lua:65: in function 'run_replace_or_original'
        ...k/packer/opt/telescope.nvim/lua/telescope/actions/mt.lua:65: in function 'run_replace_or_original'
        ...k/packer/opt/telescope.nvim/lua/telescope/actions/mt.lua:65: in function 'key_func'
        ...ack/packer/opt/telescope.nvim/lua/telescope/mappings.lua:242: in function 'execute_keymap'
        [string ":lua"]:1: in main chunk
stack traceback:
        [C]: in function 'nvim_buf_set_option'
        vim/filetype.lua: in function ''
        vim/filetype.lua: in function 'match'
        ...ocal/Cellar/neovim/0.7.0/share/nvim/runtime/filetype.lua:15: in function <...ocal/Cellar/neovim/0.7.0/share/nvim/runtime/filetype.lua:14>
        [C]: in function 'cmd'
        .../packer/opt/telescope.nvim/lua/telescope/actions/set.lua:81: in function 'edit_buffer'
        .../packer/opt/telescope.nvim/lua/telescope/actions/set.lua:148: in function 'run_replace_or_original'
        ...k/packer/opt/telescope.nvim/lua/telescope/actions/mt.lua:65: in function 'run_replace_or_original'
        ...k/packer/opt/telescope.nvim/lua/telescope/actions/mt.lua:65: in function 'run_replace_or_original'
        ...k/packer/opt/telescope.nvim/lua/telescope/actions/mt.lua:65: in function 'key_func'
        ...ack/packer/opt/telescope.nvim/lua/telescope/mappings.lua:242: in function 'execute_keymap'
        [string ":lua"]:1: in main chunk
stack traceback:
        [C]: in function 'cmd'
        .../packer/opt/telescope.nvim/lua/telescope/actions/set.lua:81: in function 'edit_buffer'
        .../packer/opt/telescope.nvim/lua/telescope/actions/set.lua:148: in function 'run_replace_or_original'
        ...k/packer/opt/telescope.nvim/lua/telescope/actions/mt.lua:65: in function 'run_replace_or_original'
        ...k/packer/opt/telescope.nvim/lua/telescope/actions/mt.lua:65: in function 'run_replace_or_original'
        ...k/packer/opt/telescope.nvim/lua/telescope/actions/mt.lua:65: in function 'key_func'
        ...ack/packer/opt/telescope.nvim/lua/telescope/mappings.lua:242: in function 'execute_keymap'
        [string ":lua"]:1: in main chunk

Yeah, maybe try it without the BufEnter event?

Thanks your solution worked.