Snacks.explorer: custom yank operation? #1372
Answered
by
drowning-cat
markhagemann
asked this question in
Q&A
Replies: 2 comments 3 replies
-
vim.env.LAZY_STDPATH = '.repro'
load(vim.fn.system 'curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua')()
---@module 'snacks'
---@diagnostic disable-next-line: missing-fields
require('lazy.minit').repro {
spec = {
{
'folke/snacks.nvim',
---@type snacks.Config
opts = {
picker = {
sources = {
explorer = {
win = {
list = {
keys = {
['Y'] = 'copy_path',
},
},
},
actions = {
copy_path = function(_, item)
local modify = vim.fn.fnamemodify
local filepath = item.file
local filename = modify(filepath, ':t')
local values = {
filepath,
modify(filepath, ':.'),
modify(filepath, ':~'),
filename,
modify(filename, ':r'),
modify(filename, ':e'),
}
local items = {
'Absolute path: ' .. values[1],
'Path relative to CWD: ' .. values[2],
'Path relative to HOME: ' .. values[3],
'Filename: ' .. values[4],
}
if vim.fn.isdirectory(filepath) == 0 then
vim.list_extend(items, {
'Filename without extension: ' .. values[5],
'Extension of the filename: ' .. values[6],
})
end
vim.ui.select(items, { prompt = 'Choose to copy to clipboard:' }, function(choice, i)
if not choice then
vim.notify 'Selection cancelled'
return
end
if not i then
vim.notify 'Invalid selection'
return
end
local result = values[i]
vim.fn.setreg('"', result) -- Neovim unnamed register
vim.fn.setreg('+', result) -- System clipboard
vim.notify('Copied: ' .. result)
end)
end,
},
},
},
},
},
},
},
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
markhagemann
-
|
For a "snacks.nvim",
keys = {
picker = {
sources = {
explorer = {
win = {
list = {
keys = {
["y"] = "yank_relative_cwd",
["Y"] = "yank_relative_home",
},
},
},
actions = {
yank_relative_cwd = function(_, item)
local path = vim.fn.fnamemodify(item.file, ":.")
vim.fn.setreg("+", path)
vim.fn.setreg('"', path)
vim.notify("Yanked: " .. path)
end,
yank_relative_home = function(_, item)
local path = vim.fn.fnamemodify(item.file, ":~")
vim.fn.setreg("+", path)
vim.fn.setreg('"', path)
vim.notify("Yanked: " .. path)
end,
},
},
},
},
}, |
Beta Was this translation helpful? Give feedback.
1 reply
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.
-
In neo-tree there was a solution given for a prompt to ask what type of yank on the filepath to do. I am just wondering if anyone has already done similar? I think the main thing I need to do is figure out what to call to get the nodes. Admittedly I haven't done a deep dive here other than checking for
relativein theexplorercode.Alternatively it'd be good to separate out
yfor relative to CWD andYfor relative to HOME.nvim-neo-tree/neo-tree.nvim#370 (comment)
The custom function from the neo-tree discussion below:
Beta Was this translation helpful? Give feedback.
All reactions