Replies: 4 comments 16 replies
-
The lack of this feature makes me hesitate to move to Neo-tree. I WANT IT! |
Beta Was this translation helpful? Give feedback.
-
require('neo-tree').setup {
window = {
mappings = {
['Y'] = function(state)
-- NeoTree is based on [NuiTree](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree)
-- The node is based on [NuiNode](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree#nuitreenode)
local node = state.tree:get_node()
local filepath = node:get_id()
local filename = node.name
local modify = vim.fn.fnamemodify
local results = {
filepath,
modify(filepath, ':.'),
modify(filepath, ':~'),
filename,
modify(filename, ':r'),
modify(filename, ':e'),
}
-- absolute path to clipboard
local i = vim.fn.inputlist({
'Choose to copy to clipboard:',
'1. Absolute path: ' .. results[1],
'2. Path relative to CWD: ' .. results[2],
'3. Path relative to HOME: ' .. results[3],
'4. Filename: ' .. results[4],
'5. Filename without extension: ' .. results[5],
'6. Extension of the filename: ' .. results[6],
})
if i > 0 then
local result = results[i]
if not result then return print('Invalid choice: ' .. i) end
vim.fn.setreg('"', result)
vim.notify('Copied: ' .. result)
end
end
}
}
} ---- Update ---- If you are using https://github.com/stevearc/dressing.nvim, try below codes. (Inspired by @mehalter 's comment) require('neo-tree').setup {
window = {
mappings = {
['Y'] = function(state)
-- NeoTree is based on [NuiTree](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree)
-- The node is based on [NuiNode](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree#nuitreenode)
local node = state.tree:get_node()
local filepath = node:get_id()
local filename = node.name
local modify = vim.fn.fnamemodify
local results = {
filepath,
modify(filepath, ':.'),
modify(filepath, ':~'),
filename,
modify(filename, ':r'),
modify(filename, ':e'),
}
vim.ui.select({
'1. Absolute path: ' .. results[1],
'2. Path relative to CWD: ' .. results[2],
'3. Path relative to HOME: ' .. results[3],
'4. Filename: ' .. results[4],
'5. Filename without extension: ' .. results[5],
'6. Extension of the filename: ' .. results[6],
}, { prompt = 'Choose to copy to clipboard:' }, function(choice)
local i = tonumber(choice:sub(1, 1))
local result = results[i]
vim.fn.setreg('"', result)
vim.notify('Copied: ' .. result)
end)
end
}
}
}
|
Beta Was this translation helpful? Give feedback.
-
Inspired by @adoyle-h <3, for anyone who wants one keystroke approach like me, and got used with nvim-tree ways (note that I remapped c to "copy_to_clipboard" instead): ["y"] = function(state)
-- NeoTree is based on [NuiTree](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree)
-- The node is based on [NuiNode](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree#nuitreenode)
local node = state.tree:get_node()
local filename = node.name
vim.fn.setreg('"', filename)
vim.notify("Copied: " .. filename)
end,
["Y"] = function(state)
-- NeoTree is based on [NuiTree](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree)
-- The node is based on [NuiNode](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree#nuitreenode)
local node = state.tree:get_node()
local filepath = node:get_id()
vim.fn.setreg('"', filepath)
vim.notify("Copied: " .. filepath)
end, |
Beta Was this translation helpful? Give feedback.
-
Inspired by @adoyle-h's comment, this adds error handling to gracefully manage cases where a user cancels the selection or makes an invalid choice: local function copy_path(state)
-- NeoTree is based on [NuiTree](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree)
-- The node is based on [NuiNode](https://github.com/MunifTanjim/nui.nvim/tree/main/lua/nui/tree#nuitreenode)
local node = state.tree:get_node()
local filepath = node:get_id()
local filename = node.name
local modify = vim.fn.fnamemodify
local results = {
filepath,
modify(filepath, ":."),
modify(filepath, ":~"),
filename,
modify(filename, ":r"),
modify(filename, ":e"),
}
vim.ui.select({
"1. Absolute path: " .. results[1],
"2. Path relative to CWD: " .. results[2],
"3. Path relative to HOME: " .. results[3],
"4. Filename: " .. results[4],
"5. Filename without extension: " .. results[5],
"6. Extension of the filename: " .. results[6],
}, { prompt = "Choose to copy to clipboard:" }, function(choice)
if choice then
local i = tonumber(choice:sub(1, 1))
if i then
local result = results[i]
vim.fn.setreg('"', result)
vim.notify("Copied: " .. result)
else
vim.notify("Invalid selection")
end
else
vim.notify("Selection cancelled")
end
end)
end Simply map the function to the desired key: window = {
mappings = {
["Y"] = copy_path,
-- Additional mappings
},
} |
Beta Was this translation helpful? Give feedback.
-
Hi, what do you think about adding a feature like nvim-tree's
copy_name
andcopy_path
?Beta Was this translation helpful? Give feedback.
All reactions