Is it possible to allow to select current buffer in FzfLua buffers? #1222
Replies: 4 comments 4 replies
-
Ty for the kind words @lucassperez. You can make the first buffer selectable in two ways: :FzfLua buffers fzf_opts.--header-lines=false Or by disabling the sorting by most recent used: :FzfLua buffers sort_lastused=false
However you still won’t be able to close the current buffer, that would require additional logic of first switching to a different buffer (or creating a new buffer if you have just one buffer), the logic can be a bit more nuanced than this and not something I intend to add. If you wish to do that create your own custom buffer delete action that at the most basic level switches to the alternate buffer (ctrl-6) before closing the currently displayed buffer. |
Beta Was this translation helpful? Give feedback.
-
I see. Thanks for the explanation, and also such a very fast reply. Apparently it is more complicated than I thought, but just being able to see the current buffer is something that I like. I added |
Beta Was this translation helpful? Give feedback.
-
Just did this - it creates a scratch buffer if you close the current buffer. Let me know if you have any feedback on the code - first time writing an action. buffers = {
fzf_opts = {
["--header-lines"] = false,
},
actions = {
["ctrl-x"] = {
fn = function(selected, opts)
for _, sel in ipairs(selected) do
local entry = path.entry_to_file(sel, opts)
local bufnr = entry.bufnr
local is_dirty = utils.buffer_is_dirty(bufnr, true, false)
local save_dialog = function()
return utils.save_dialog(bufnr)
end
if bufnr and (not is_dirty or vim.api.nvim_buf_call(bufnr, save_dialog)) then
-- The current buffer is actually the picker - so the alt buffer lets
-- us see the file we're currently editing
local current_buf = vim.fn.bufnr("#")
if bufnr == current_buf then
-- Current buffer is going to be deleted, so we'll need to
-- replace it with the scratch buf in all windows
local windows = vim.fn.win_findbuf(current_buf)
local new_buf = vim.api.nvim_create_buf(false, true)
for _, win in ipairs(windows) do
vim.api.nvim_win_set_buf(win, new_buf)
end
end
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end
end,
},
},
}, |
Beta Was this translation helpful? Give feedback.
-
Mocked up an alternate version that goes to the previous buffer, rather than creating a scratch buf. Would appreciate feedback on this one - it required a more in-depth impl, since I needed a way to access the previous buffer - but since we're in the picker, the alternate file is the current buffer, and there doesn't seem to be a native API for going two buffers back. To get around this, I had to construct that list myself. local function get_sorted_buflist()
local info = vim.fn.getbufinfo()
-- Sort on how recently the buffer was used
info = vim.fn.sort(info, function(a, b)
return a.lastused <= b.lastused
end)
info = vim.tbl_filter(function(current)
return current.listed == 1 and current.hidden == 0
end, info)
-- Take the full info and turn it into just the bufnrs
local bufnrs = {}
for index, current in ipairs(info) do
bufnrs[index] = current.bufnr
end
return bufnrs
end
-- Custom action that allows deleting the current buffer. If you do, it swaps to
-- the last buffer you used!
local function delete_buffer_action(selected, opts)
-- List of buffers sorted by how frequently we accessed them.
local sorted_buflist = get_sorted_buflist()
local current_buf = sorted_buflist[1]
for _, sel in ipairs(selected) do
local entry = path.entry_to_file(sel, opts)
local bufnr = entry.bufnr
-- If the current file has unsaved changes, prompt the user to save
local is_dirty = utils.buffer_is_dirty(bufnr, true, false)
local save_dialog = function()
return utils.save_dialog(bufnr)
end
if bufnr and (not is_dirty or vim.api.nvim_buf_call(bufnr, save_dialog)) then
if bufnr == current_buf then
local windows = vim.fn.win_findbuf(current_buf)
-- Buffer we accessed most recently after the current buf. We can't use
-- alternate file here, because the REAL current buf is actually the
-- picker - so we need to go two files back. This is why we created a
-- sorted buflist in the first place.
local new_buf = sorted_buflist[2]
for _, win in ipairs(windows) do
vim.api.nvim_win_set_buf(win, new_buf)
end
end
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end
end |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First of all, I think this plugin is awesome and your work is awesome! (:
But my question/ideia is this:
When using the command
FzfLua buffers
, it is possible to do some actions to buffers. By default, we can close buffers with<C-x>
. I find this to be very handy, but also a little strange that we cannot select the current buffer, making us unable to close the current buffer via this command. Sometimes I might want to use FzfLua to do some cleaning on open buffers and if I'm randomly on a buffer I find out I actually want to close, I can't do it now.Is there a way to allow this?
Beta Was this translation helpful? Give feedback.
All reactions