summaryrefslogtreecommitdiffstats
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
commite32bbde3df076efeadb742a185bb2297d411e5be (patch)
tree89364ecb1257db860d58c607d5b5d349976791da
parentb152921515b4892609516fd5cf8e538de66902ef (diff)
downloaddotfiles-e32bbde3df076efeadb742a185bb2297d411e5be.tar.gz
dotfiles-e32bbde3df076efeadb742a185bb2297d411e5be.zip
misc(nvim): prompt user for action when buffer file is edited outside of nvim
-rw-r--r--.config/nvim/init.lua27
1 files changed, 20 insertions, 7 deletions
diff --git a/.config/nvim/init.lua b/.config/nvim/init.lua
index 792c181..00b2b1f 100644
--- a/.config/nvim/init.lua
+++ b/.config/nvim/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,
})