-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support trash instead of delete #202
Comments
This would be a simple one for a custom command. Take a look at Something like this should work (from memory, not tested): require("neo-tree").setup({
filesystem = {
window = {
mappings = {
["D"] = functions(state)
local node = state.tree:get_node()
local path = node:get_id()
vim.fn.system("trash " .. vim.fn.fnameescape(path))
end
}
}
}
})
|
Thanks for your answer, I did go with this: require("neo-tree").setup({
filesystem = {
commands = {
-- Override delete to use trash instead of rm
delete = function(state)
local path = state.tree:get_node().path
vim.fn.system({ "trash", vim.fn.fnameescape(path) })
end,
},
},
}) It did work and the docs in In the mean time: Can you please tell me how to refresh the tree after the operation is done? Thank you a lot for the quick rely! |
You can use require("neo-tree").setup({
filesystem = {
commands = {
-- Override delete to use trash instead of rm
delete = function(state)
local path = state.tree:get_node().path
vim.fn.system({ "trash", vim.fn.fnameescape(path) })
require("neo-tree.sources.manager").refresh(state.name)
end,
},
},
}) |
Looking at the code now, there is some extra niceties happening that is worth having. I will leave this up as an enhancement, but since I don't use trash I'm not the best person to implement it. Hopefully someone else will pick it up. |
Refreshing did work. Thank you! |
To extend #202 (comment) the example below adds a confirmation dialog. What's still open is using the trash when deleting a range of files. But usually git should lull one into safety without the need for a trash. delete = function(state)
local inputs = require "neo-tree.ui.inputs"
local path = state.tree:get_node().path
local msg = "Are you sure you want to delete " .. path
inputs.confirm(msg, function(confirmed)
if not confirmed then return end
vim.fn.system { "trash", vim.fn.fnameescape(path) }
require("neo-tree.sources.manager").refresh(state.name)
end)
end, |
To work with multi selected nodes (in Visual mode), I also extended tobealive's code to below. -- over write default 'delete' command to 'trash'.
delete = function(state)
local inputs = require("neo-tree.ui.inputs")
local path = state.tree:get_node().path
local msg = "Are you sure you want to trash " .. path
inputs.confirm(msg, function(confirmed)
if not confirmed then return end
vim.fn.system { "trash", vim.fn.fnameescape(path) }
require("neo-tree.sources.manager").refresh(state.name)
end)
end,
-- over write default 'delete_visual' command to 'trash' x n.
delete_visual = function(state, selected_nodes)
local inputs = require("neo-tree.ui.inputs")
-- get table items count
function GetTableLen(tbl)
local len = 0
for n in pairs(tbl) do
len = len + 1
end
return len
end
local count = GetTableLen(selected_nodes)
local msg = "Are you sure you want to trash " .. count .. " files ?"
inputs.confirm(msg, function(confirmed)
if not confirmed then return end
for _, node in ipairs(selected_nodes) do
vim.fn.system { "trash", vim.fn.fnameescape(node.path) }
end
require("neo-tree.sources.manager").refresh(state.name)
end)
end, nvim's reboot is required for that 'refresh' command to work. Thanks for all your advices. |
@riodelphino Thanks for sharing, helped me a lot. |
I just deleted 18 hours of work by mistake. Its not reasonable to delete files forever with |
I mean... You literally have to confirm you want to delete the thing. So while you may not like to hear this, this is purely on you. If you select delete, and confirm you do want to delete, you cannot be upset that it was deleted. This idea that your editor should be responsible for the user's mistakes is incorrect. Additionally as was pointed out above, the support to do this is already built into neo-tree. I will say we should probably put this recipe into the wiki, but IMO that is as far as this should go. Supporting trash is not as easy as it sounds (remember that there are 3 major operating systems, and 2 of them have completely different ways of doing trash support). At the end of the day, Neovim and in extension Neo-tree are meant to be customized by you the user. Make it what you want. If you want the But we should not be changing the default simply because of a mistake confirmed by a few people. I'm sorry you lost all that work. And I know this will read quite harsh. The intention is not to shame you, but simply to explain my position in saying we should not be changing the default on this behavior. Delete should be delete. |
Thank you for addressing my comment, I appreciate your thoughts and words. My mistake is that I used visual mode to delete 20+ files, mistakenly the cursor overlapped to the folder which I didn't want to deleted. I'm sure you can understand how easily this can happen to others. I understand that this is not easy to add support for all OS's, and agree it should be added to the wiki. If this is not going to be supported out of the box it should be mentioned in the README with a big caution notice so each user can address it according to the OS he operates on. |
Just had a slightly different accident with deleting files #1090 Since shite like this and nasty bugs could happen and implementing a saver delete seems to be problematic, my (sad) solution is to switch back to the much saver nnn integration.
@amitlevy21 Yep! |
{
"nvim-neo-tree/neo-tree.nvim",
opts = {
filesystem = {
commands = {
-- Override delete to use trash instead of rm
delete = function(state)
local inputs = require("neo-tree.ui.inputs")
local path = state.tree:get_node().path
local msg = "Are you sure you want to delete "
.. path
inputs.confirm(msg, function(confirmed)
if not confirmed then
return
end
vim.fn.system({
"trash",
vim.fn.fnameescape(path),
})
require("neo-tree.sources.manager").refresh(
state.name
)
end)
end,
},
},
},
}, |
Hi. It passed long time, so now I'm not sure about my and your code. |
Worked! |
When using @riodelphino 's script, I've met with a problem that if there are spaces in the path, the file could not be trashed. After doing some tests, I figured out that If anyone encounters the same issue, simply remove the |
I've created the following configuration: This setup defines the The key mappings for local utils = require("neo-tree.utils")
return {
"nvim-neo-tree/neo-tree.nvim",
opts = {
filesystem = {
window = {
mappings = {
["d"] = "delete",
["D"] = "trash",
},
},
commands = {
trash = function(state)
local inputs = require("neo-tree.ui.inputs")
local path = state.tree:get_node().path
local _, name = utils.split_path(path)
local msg = string.format("Are you sure you want to trash '%s'?", name)
inputs.confirm(msg, function(confirmed)
if not confirmed then
return
end
vim.fn.system({ "trash", vim.fn.fnameescape(path) })
require("neo-tree.sources.manager").refresh(state.name)
end)
end,
trash_visual = function(state, selected_nodes)
local inputs = require("neo-tree.ui.inputs")
local msg = "Are you sure you want to trash " .. #selected_nodes .. " files ?"
inputs.confirm(msg, function(confirmed)
if not confirmed then
return
end
for _, node in ipairs(selected_nodes) do
vim.fn.system({ "trash", vim.fn.fnameescape(node.path) })
end
require("neo-tree.sources.manager").refresh(state.name)
end)
end,
},
},
},
} |
Nice work ! |
And now I want the additional code that notice a trash runtime error like below. If I execute trash / trash_visual in icloud's sync folder like
I'm thinking of writing some code. Additionally: |
Here is the code, notice simple message on trash error. commands = {
trash = function(state)
local inputs = require('neo-tree.ui.inputs')
local path = state.tree:get_node().path
local utils = require('neo-tree.utils')
local _, name = utils.split_path(path)
local msg = string.format("Are you sure you want to trash '%s'?", name)
inputs.confirm(msg, function(confirmed)
if not confirmed then return end
pcall(function()
vim.fn.system({ 'trash', vim.fn.fnameescape(path) })
if vim.v.shell_error ~= 0 then
msg = 'trash command failed.'
vim.notify(msg, vim.log.levels.ERROR, { title = 'Neo-tree' })
end
end)
require('neo-tree.sources.manager').refresh(state.name)
end)
end,
trash_visual = function(state, selected_nodes)
local inputs = require('neo-tree.ui.inputs')
local msg = 'Are you sure you want to trash ' .. #selected_nodes .. ' files ?'
inputs.confirm(msg, function(confirmed)
if not confirmed then return end
for _, node in ipairs(selected_nodes) do
pcall(function()
vim.fn.system({ 'trash', vim.fn.fnameescape(node.path) })
if vim.v.shell_error ~= 0 then
msg = 'trash command failed.'
vim.notify(msg, vim.log.levels.ERROR, { title = 'Neo-tree' })
end
end)
end
require('neo-tree.sources.manager').refresh(state.name)
end)
end,
},
}, If you want replace the original message with vim.notify = require('notify') |
Thanks for the awesome plugin!
Adding support for
trash
instead ofrm
would be awesome!Nvim-Tree does this with the
D
shortcut.The text was updated successfully, but these errors were encountered: