diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-05-22 16:57:15 +0200 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-05-22 17:01:02 +0200 |
| commit | ff3b3a1ce51ccae0958de43608743cb2a4794866 (patch) | |
| tree | 37a5b04da6573744bfd6d6d50c6b6c064d822dd6 /init.lua | |
| parent | adb5bc477bad8f4d2789e75a37f36663d219bca3 (diff) | |
| download | nvim-config-ff3b3a1ce51ccae0958de43608743cb2a4794866.tar.gz nvim-config-ff3b3a1ce51ccae0958de43608743cb2a4794866.zip | |
misc(nvim): prompt user for action when buffer file is edited outside of nvim
Diffstat (limited to 'init.lua')
| -rw-r--r-- | init.lua | 27 |
1 files changed, 20 insertions, 7 deletions
@@ -64,20 +64,33 @@ vim.api.nvim_create_autocmd({ "WinEnter", "TermOpen" }, { end, }) --- Reload buffer when file has been modified outside of nvim -vim.api.nvim_create_autocmd({ "FocusGained", "BufEnter", "CursorHold", "CursorHoldI" }, { - desc = "Reload buffer when modified outside of nvim", +-- Reload file when changed outside of nvim +vim.opt.autoread = false -- Must be false for FileChangedShell to trigger when the buffer was not changed +-- The manual says that FileChangedShell is triggered on FocusGained but it's not true. This function actually triggers +-- the next FileChangedShell when the buffer has unsaved changes and the file was modified outside of nvim. +vim.api.nvim_create_autocmd({ "FocusGained" }, { + desc = "Trigger buffer reload when file is edited outside of nvim", + group = vim.g.dotfiles.augroup, + callback = function() + vim.cmd.checktime() + end, +}) +vim.api.nvim_create_autocmd({ "FileChangedShell" }, { + desc = "Prompt user to reload buffer when modified outside of nvim", group = vim.g.dotfiles.augroup, pattern = "*", - command = "checktime", + callback = function(opts) + if vim.v.fcs_reason == "conflict" or vim.v.fcs_reason == "changed" then + vim.v.fcs_choice = "ask" + end + end, }) --- Log a message when buffer is reloaded because the file has changed on disk outside of nvim vim.api.nvim_create_autocmd({ "FileChangedShellPost" }, { desc = "Notify buffer reloaded because its file has changed on disk", group = vim.g.dotfiles.augroup, pattern = "*", - callback = function() - vim.notify("File changed on disk, buffer reloaded", vim.log.levels.WARN) + callback = function(opts) + vim.notify("File " .. opts.file .. " has changed on disk", vim.log.levels.WARN) end, }) |
