From 2c3d30d1ae1fb638636097567d4804671e876d24 Mon Sep 17 00:00:00 2001 From: Franco Morroni Date: Sun, 9 Aug 2026 04:10:49 -0300 Subject: [PATCH] feat(picker.preview): add option to opt out of real-buffer preview Picker previews can be of two types: scratch buffers or real buffers. Scratch buffers are used when the file isn't already open, while real buffers are used when the file is already loaded. Using a real buffer means the preview inherits the buffer's existing state and keymaps, which can interfere with picker-specific keymaps (see https://github.com/folke/snacks.nvim/issues/1417). It also updates the buffer's last-used timestamp, which can affect picker sorting (see https://github.com/folke/snacks.nvim/issues/773). Add a `use_real_buffer` option to allow users to opt out of real-buffer previews and always use scratch buffers instead. --- lua/snacks/picker/config/defaults.lua | 1 + lua/snacks/picker/preview.lua | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lua/snacks/picker/config/defaults.lua b/lua/snacks/picker/config/defaults.lua index 77f9e3c9c..95dac64b4 100644 --- a/lua/snacks/picker/config/defaults.lua +++ b/lua/snacks/picker/config/defaults.lua @@ -198,6 +198,7 @@ local defaults = { max_size = 1024 * 1024, -- 1MB max_line_length = 500, -- max line length ft = nil, ---@type string? filetype for highlighting. Use `nil` for auto detect + use_real_buffer = true ---@type boolean use actual buffer as preview when the file is already loaded (default: true) }, man_pager = nil, ---@type string? MANPAGER env to use for `man` preview }, diff --git a/lua/snacks/picker/preview.lua b/lua/snacks/picker/preview.lua index 462b9aa03..4e465e431 100644 --- a/lua/snacks/picker/preview.lua +++ b/lua/snacks/picker/preview.lua @@ -101,7 +101,7 @@ function M.file(ctx) vim.fn.bufload(ctx.item.buf) end - if ctx.item.buf and vim.api.nvim_buf_is_loaded(ctx.item.buf) then + if ctx.item.buf and vim.api.nvim_buf_is_loaded(ctx.item.buf) and ctx.picker.opts.previewers.file.use_real_buffer then if not title then local name = vim.api.nvim_buf_get_name(ctx.item.buf) title = uv.fs_stat(name) and vim.fn.fnamemodify(name, ":t") or name