Paste and rename functionality (single file) or duplicate #1748
lalvarezt
started this conversation in
Show and tell
Replies: 2 comments
-
another option is to modify the return {
"folke/snacks.nvim",
opts = {
picker = {
actions = {
explorer_copy_default = function(picker, item)
if not item then
return
end
local Tree = require("snacks.explorer.tree")
local actions = require("snacks.explorer.actions")
local uv = vim.uv or vim.loop
---@type string[]
local paths = vim.tbl_map(Snacks.picker.util.path, picker:selected())
-- Copy selection
if #paths > 0 then
local dir = picker:dir()
Snacks.picker.util.copy(paths, dir)
picker.list:set_selected() -- clear selection
Tree:refresh(dir)
Tree:open(dir)
actions.update(picker, { target = dir })
return
end
Snacks.input({
prompt = "Copy to",
default = vim.fn.fnamemodify(item.file, ":t"),
}, function(value)
if not value or value:find("^%s$") then
return
end
local dir = vim.fs.dirname(item.file)
local to = svim.fs.normalize(dir .. "/" .. value)
if uv.fs_stat(to) then
Snacks.notify.warn("File already exists:\n- `" .. to .. "`")
return
end
Snacks.picker.util.copy_path(item.file, to)
Tree:refresh(vim.fs.dirname(to))
actions.update(picker, { target = to })
end)
end,
explorer_paste_rename = function(picker)
local Tree = require("snacks.explorer.tree")
local actions = require("snacks.explorer.actions")
local files = vim.split(vim.fn.getreg(vim.v.register or "+") or "", "\n", { plain = true })
files = vim.tbl_filter(function(file)
return file ~= "" and vim.fn.filereadable(file) == 1
end, files)
if #files == 0 then
return Snacks.notify.warn(("The `%s` register does not contain any files"):format(vim.v.register or "+"))
elseif #files == 1 then
local file = files[1]
local base = vim.fn.fnamemodify(file, ":t")
local dir = picker:dir()
Snacks.input({
prompt = "Rename pasted file",
default = base,
}, function(value)
if not value or value:find("^%s*$") then
return
end
local uv = vim.uv or vim.loop
local target = vim.fs.normalize(dir .. "/" .. value)
if uv.fs_stat(target) then
return Snacks.notify.warn("File already exists:\n- `" .. target .. "`")
end
Snacks.picker.util.copy_path(file, target)
Tree:refresh(dir)
Tree:open(dir)
actions.update(picker, { target = target })
end)
else
local dir = picker:dir()
local uv = vim.uv or vim.loop
for _, file in ipairs(files) do
local base = vim.fn.fnamemodify(file, ":t")
local target = vim.fs.normalize(dir .. "/" .. base)
local name, ext = base:match("^(.*)%.(.*)$")
name = name or base
ext = ext and ("." .. ext) or ""
local count = 1
while uv.fs_stat(target) do
target = vim.fs.normalize(dir .. "/" .. name .. "_" .. count .. ext)
count = count + 1
end
Snacks.picker.util.copy_path(file, target)
end
Tree:refresh(dir)
Tree:open(dir)
actions.update(picker, { target = dir })
end
end,
},
sources = {
explorer = {
win = {
list = {
keys = {
["R"] = "explorer_paste_rename",
["C"] = "explorer_copy_default",
},
},
},
},
},
},
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Amazing solution; I just adopted this to my setup. Thank you very much I have also discovered that Yank then copy ( |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
probably not the best way to go about it but seems to work
more info here #903
Beta Was this translation helpful? Give feedback.
All reactions