Deleting scratch buffers #357
-
Greetings, What would be the best way of deleting old/unused scratch buffers? Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Either manually or create a custom picker for the fuzzy finder you use to do this. I use fzf-lua and have the following custom picker local fzf = require("fzf-lua")
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
function M.scratch_buffers()
local entries = {}
local items = Snacks.scratch.list()
local item_map = {}
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)
table.insert(entries, display)
item_map[display] = item
end
fzf.fzf_exec(entries, {
winopts = {
height = math.floor(math.min(vim.o.lines * 0.8, #entries + 3) + 0.5),
width = 0.5,
},
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]]
---@diagnostic disable-next-line: missing-fields
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)
M.scratch_buffers()
end,
},
})
end You could probably do the same for Telescope, but I don't use it and can't be of help there. |
Beta Was this translation helpful? Give feedback.
-
Taken from another comment I made in #207: This works for me: return {
"folke/snacks.nvim",
opts = {
scratch = {
win = {
keys = {
["delete"] = {
"<a-x>",
function(self)
vim.api.nvim_win_call(self.win, function()
vim.cmd([[close]])
os.remove(vim.api.nvim_buf_get_name(self.buf))
end)
end,
desc = "Delete buffer",
mode = { "n", "x" },
},
},
},
},
},
} |
Beta Was this translation helpful? Give feedback.
Either manually or create a custom picker for the fuzzy finder you use to do this. I use fzf-lua and have the following custom picker