nvim-tree / nvim-tree.lua

A file explorer tree for neovim written in lua
Other
7.17k stars 610 forks source link

copy files between multiple vim instances #1549

Open otavioschwanck opened 2 years ago

otavioschwanck commented 2 years ago

i use use neovim instance for each of projects of mine. sometimes i want to copy file from one to another and its really hard to do with nvim-tree clipboard working only current instance

alex-courtis commented 2 years ago

nvim-tree does not use the system clipboard.

An optional protocol could be developed that uses system clipboard. Something like:

nvim-tree: /path/to/file1, /path/to/file2

This would cause strange results when pasting the clipboard anywhere outside of nvim-tree, and files copied outside of nvim-tree could not be pasted in nvim-tree.

It is highly unlikely that this functionality will be built or accepted.

otavioschwanck commented 2 years ago

it could be an optional feature that could be enabled, i really dont care about the system clipboard, even the filebrowser from the OS works in this way, and also, nvim-tree could save on a pre-defined register (i think it is shared between vim instances)

Shougo commented 2 years ago

What is the pre-defined register? clipboard register is only shared. The other registers are not shared.

otavioschwanck commented 2 years ago

What is the pre-defined register? clipboard register is only shared. The other registers are not shared.

you are right!

But it could save on a tmp file

alex-courtis commented 2 years ago

But it could save on a tmp file

That is a viable solution.

hinell commented 1 year ago

One can obtain current file name by using expand("%:p") builtin. Then, when copied into a register (clipboard) you can copy it via commandline terminal:


" In vim commandline
:echo setreg("", expand("%:p"))
" Switch to another window
:!cp ...<ctrl+v> ./
hinell commented 1 year ago

But it could save on a tmp file

That is a viable solution.

@alex-courtis You are better to keep track of a file path, rather than copying it somehwere temporarily.

lawrence-laz commented 1 year ago

We already have gy to copy the absolute path. What if we had something like gp to paste from the absolute path?

The second step could be to use m to mark multiple files, then go bgy to copy all absolute paths with \n delimiter and make gp be capable of pasting from multiple paths.

What do you think?

alex-courtis commented 1 year ago

We already have gy to copy the absolute path. What if we had something like gp to paste from the absolute path?

That's an interesting idea - using absolute paths rather than the "internal clipboard". The question remains: where do these paths come from? The " or + register?

The second step could be to use m to mark multiple files, then go bgy to copy all absolute paths with \n delimiter and make gp be capable of pasting from multiple paths.

Could we not simplify this to something like: m, bcp?

lawrence-laz commented 1 year ago

We already have gy to copy the absolute path. What if we had something like gp to paste from the absolute path?

That's an interesting idea - using absolute paths rather than the "internal clipboard". The question remains: where do these paths come from? The " or + register?

I'd say they should act the same way as gy currently does, i. e. based on user settings:

local function copy_to_clipboard(content)
if M.use_system_clipboard == true then
vim.fn.setreg("+", content)
vim.fn.setreg('"', content)
return notify.info(string.format("Copied %s to system clipboard!", content))
else
vim.fn.setreg('"', content)
vim.fn.setreg("1", content)
return notify.info(string.format("Copied %s to neovim clipboard!", content))
end
end

The second step could be to use m to mark multiple files, then go bgy to copy all absolute paths with \n delimiter and make gp be capable of pasting from multiple paths.

Could we not simplify this to something like: m, bcp?

That could work too. I just like bgy as I personally find it easier to remember as a variant of gy that works with bookmarks. Also, since both gy and bgy could potentially work with gp interchangeably for single file or multiple files, it would be less confusing to have similar commands maybe. I don't have a strong opinion here though.

jgarciaokode commented 1 year ago

Hi, finally, is this possible? This would be such a useful utility, open vertical tmux pane, open another git project, and then copy files from one project to another like in vscode

alex-courtis commented 1 year ago

This has not been implemented however we do have the bones of a solution here.

Pull Requests are most gratefully appreciated.

lawrence-laz commented 1 year ago

This is nowhere PR ready, but maybe someone will find this useful or flesh it out into a PR:

vim.api.nvim_create_autocmd('filetype', {
    pattern = 'NvimTree',
    desc = 'Mappings for NvimTree',
    callback = function()
        -- Yank marked files
        vim.keymap.set('n', 'bgy',
            function()
                local api = require 'nvim-tree.api'
                local marks = api.marks.list()
                if #marks == 0 then
                    print('No items marked')
                    return
                end
                local absolute_file_paths = ''
                for _, mark in ipairs(marks) do
                    absolute_file_paths = absolute_file_paths .. mark.absolute_path .. '\n'
                end
                -- Using system registers for multi-instance support.
                vim.fn.setreg("+", absolute_file_paths)
                print('Yanked ' .. #marks .. ' items')
            end,
            { remap = true, buffer = true })

        -- Paste files
        vim.keymap.set('n', 'gp',
            function()
                local api = require 'nvim-tree.api'
                local source_paths = {}
                for path in vim.fn.getreg('+'):gmatch('[^\n%s]+') do
                    source_paths[#source_paths + 1] = path
                end
                local node = api.tree.get_node_under_cursor()
                local is_folder = node.fs_stat and node.fs_stat.type == 'directory' or false
                local target_path = is_folder and node.absolute_path or
                    vim.fn.fnamemodify(node.absolute_path, ":h")
                for _, source_path in ipairs(source_paths) do
                    vim.fn.system { 'cp', '-R', source_path, target_path }
                end
                api.tree.reload()
                print('Pasted ' .. #source_paths .. ' items')
            end,
            { remap = true, buffer = true })
    end
})

output

alex-courtis commented 1 year ago

Nice work! The concept is proven.

" + does indeed seem the best solution.

This can be added everywhere - copy/cut as well as bulk

jugarpeupv commented 9 months ago

Hi @alex-courtis is this planned to be added? I am currently using the snippet code from @lawrence-laz and it works really well

alex-courtis commented 9 months ago

Pull Requests are always most gratefully appreciated.