Skip to content

feat(picker.preview): add option to opt out of real-buffer preview - #2927

Open
fmorroni wants to merge 1 commit into
folke:mainfrom
fmorroni:optional-real-buffer-preview
Open

feat(picker.preview): add option to opt out of real-buffer preview#2927
fmorroni wants to merge 1 commit into
folke:mainfrom
fmorroni:optional-real-buffer-preview

Conversation

@fmorroni

@fmorroni fmorroni commented Aug 9, 2026

Copy link
Copy Markdown

Description

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 #1417). It also updates the buffer's last-used timestamp, which can affect picker sorting (see #773).

For my use cases, having the real-buffer preview has no benefits. Other users also seem to feel the same way (#773 (comment)).

This PR adds a use_real_buffer option to allow users to opt out of real-buffer previews and always use scratch buffers instead. You would opt out by setting picker.previewers.file.use_real_buffer = false.

Related Issue(s)

#1417
#773

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
folke#1417). It also updates the
buffer's last-used timestamp, which can affect picker sorting (see
folke#773).

Add a `use_real_buffer` option to allow users to opt out of real-buffer
previews and always use scratch buffers instead.
@github-actions github-actions Bot added picker size/s Small PR (<10 lines changed) labels Aug 9, 2026
@dpetka2001

Copy link
Copy Markdown
Contributor

You might also want to make the following addition

diff --git a/lua/snacks/picker/source/buffers.lua b/lua/snacks/picker/source/buffers.lua
index f7a7ee7a..47e9f349 100644
--- a/lua/snacks/picker/source/buffers.lua
+++ b/lua/snacks/picker/source/buffers.lua
@@ -47,6 +47,9 @@ function M.buffers(opts, ctx)
   end
   if opts.sort_lastused then
     table.sort(items, function(a, b)
+      if a.info.lastused == b.info.lastused then
+        return a.buf < b.buf
+      end
       return a.info.lastused > b.info.lastused
     end)
   end

because otherwise the order will still be non-deterministic for same lastused timestamps. For example I opened Neovim with multiple buffers, but only the first one was loaded. The rest had lastused = 0 and table.sort does not return the same order if you delete a buffer for example. And after you delete a buffer the picker refreshes and table.sort sorts items again but possibly not with same order always if all have same lastused value. We need a tiebreaker.

@fmorroni

fmorroni commented Aug 9, 2026

Copy link
Copy Markdown
Author

Hmm yeah that makes sense. Although I think that change should be added regardless of whether this PR is accepted or not, so maybe it should go in its own PR?

That being said, I have no issues with adding it here if that seems best.

@dpetka2001

Copy link
Copy Markdown
Contributor

I'm just a simple user myself like you. I just made the suggestion because you mentioned in your OP about how lastused affects picker sort order. Without this change the sort order will still be affected.

Whether you want to make a different PR or add it here is up to you. Not making any decisions myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

picker size/s Small PR (<10 lines changed)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants