summaryrefslogtreecommitdiffstats
path: root/init.lua
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-05-22 16:57:15 +0200
committerThomas Vanbesien <tvanbesi@proton.me>2026-05-22 17:01:02 +0200
commitff3b3a1ce51ccae0958de43608743cb2a4794866 (patch)
tree37a5b04da6573744bfd6d6d50c6b6c064d822dd6 /init.lua
parentadb5bc477bad8f4d2789e75a37f36663d219bca3 (diff)
downloadnvim-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.lua27
1 files changed, 20 insertions, 7 deletions
diff --git a/init.lua b/init.lua
index 792c181..00b2b1f 100644
--- a/init.lua
+++ b/init.lua
@@ -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,
})