Hi,
I have a new feature request which actually I'm missing a lot. Often I have to copy filepath of the file on which I'm working now and send it to a friend. I want to create a custom mapping yp for it, but I wasn't able to do that because of my low lua skills.
I found out that in /nvim-tree/lib.lua you have function get_node_at_cursor(). Probably it can be used to get _absolute_path_ of current node at cursor and then save this path to the clipboard (main register). Unfortunately, I'm not able to that because I don't know lua so good, but I think implementation would be easy. It would be nice to get some information that file of path ... has been copied to clipboard like:
api.nvim_out_write(node.absolute_path..' filepath added to clipboard.\n')
or sth similar.
Could someone of you add this kind of feature?
Thank you a lot!
You could call a lua function (by defining a config keymap basically). something like:
lua require'nvim-tree'.lib.get_node_at_cursor().absolute_path
vim.cmd(":!echo "..path.." | whatever_binary_you_use")
You helped me a little bit with that and I was able to resolve it. Just my suggestion, anyway I would add it to plugin itself in extended version to make it work on all OS but for me below implementation is enough.
I created new lua file with function:
local api = vim.api
local M = {
path = nil
}
M.copy_path_to_clipboard = function()
M.path = require'nvim-tree.lib'.get_node_at_cursor().absolute_path
os.execute("echo "..M.path.." | xclip -selection clipboard")
api.nvim_out_write(M.path..' filepath added to clipboard.\n')
end
return M
Then I added new keybinding in nvim-tree.config:
["yp"] = ":lua require'utils'.copy_path_to_clipboard()<cr>",
utils is my utils.lua file in main lua folder of my nvim config.
Probably there is another way to do it without os.execute and then it will be faster but works as expected now.
Also for anyone else who needs it in my case I didn't need my HOME path to be copied within the absolute path so I truncated it:
M.home_dir = os.getenv("HOME")..'/'
M.path = require'nvim-tree.lib'.get_node_at_cursor().absolute_path:gsub(M.home_dir, '')
I understand but this is the kind of features users can do themselves, thats why I prefer leaving them the choice. Even on linux I use both x11 and wayland, so that would make the whole compatibility harder. also I cannot test the feature in macos or windows si I prefer not to implement it :)
Maybe start a wiki page with such user configurations?
wouldn't be a bad idea, i'll do that soon