Quick way to remove scratch buffers? #207
-
The more I use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
One solution would be to add a parameter to Since you use FzfLua, you can take make your own custom picker by using the Scratch API to get the list of items. Something like the following should work I believe function M.scratch_delete()
local entries = {} -- use this to populate the table with single string, because FzfLua expects items to be strings and not an object like `M.list` returns
local items = Snacks.scratch.list()
local item_map = {} -- use this to store `item`, so we can later get access to `item.file`
local utils = require("fzf-lua.utils")
local function hl_validate(hl)
return not utils.is_hl_cleared(hl) and hl or nil
end
local function ansi_from_hl(hl, s)
return utils.ansi_from_hl(hl_validate(hl), s)
end
for _, item in ipairs(items) do
item.icon = item.icon or Snacks.util.icon(item.ft, "filetype")
item.branch = item.branch and ("branch:%s"):format(item.branch) or ""
item.cwd = item.cwd and vim.fn.fnamemodify(item.cwd, ":p:~") or ""
local display = string.format("%s %s %s %s", item.cwd, item.icon, item.name, item.branch) -- same as what Snacks.scratch uses to display items
table.insert(entries, display)
item_map[display] = item
end
local fzf = require("fzf-lua")
fzf.fzf_exec(entries, {
prompt = "Scratch Buffers",
fzf_opts = {
["--header"] = string.format(
":: <%s> to %s | <%s> to %s",
ansi_from_hl("FzfLuaHeaderBind", "enter"),
ansi_from_hl("FzfLuaHeaderText", "Select Scratch"),
ansi_from_hl("FzfLuaHeaderBind", "ctrl-d"),
ansi_from_hl("FzfLuaHeaderText", "Delete Scratch")
),
},
actions = {
["default"] = function(selected)
local item = item_map[selected[1]]
Snacks.scratch.open({ icon = item.icon, file = item.file, name = item.name, ft = item.ft })
end,
["ctrl-d"] = function(selected)
local item = item_map[selected[1]]
os.remove(item.file)
vim.notify("Deleted scratch file: " .. item.file)
end,
},
})
end And you can add additional actions according to your preferences. I believe the tools are there and each user should create his own custom picker to do what he wants. Bind it to a keymap and it should do what you want vim.keymap.set("n", "<leader>D", function()
Util.fzf.scratch_delete() -- this is just the path I use to store my custom pickers for fzf, change that to wherever you will store the above function
end, { desc = "Delete scratch file" }) |
Beta Was this translation helpful? Give feedback.
snacks
usesvim.ui.select
to list available Scratch buffers, but as far as I know it doesn't accept actions, like FzfLua or Telescope.One solution would be to add a parameter to
M.select(mode)
and then check for example if it is"delete"
to execute some additional logic to delete the file. But, in my personal opinion, that would soon escalate to other requests about additional actions and maybe that would be out of scope in the end.Since you use FzfLua, you can take make your own custom picker by using the Scratch API to get the list of items. Something like the following should work I believe