Copy path file with NERDtree Vim plugin
Asked Answered
A

3

15

Is there some way to copy the path of a file in the Vim's NERDtree plugin?

Better: is there some plugin to make the same operations the SideBarEnhancements plugin of Sublime Text does?

Ashling answered 3/5, 2013 at 23:5 Comment(1)
Please edit your question to explain a) what that ST plugin does and b) why the equivalent features in Vim don't fit the bill. Also, what do you want to do with that file name once it is copied? Insert it in the current buffer? Paste it in another program? Beware of the XY problem.Hurdle
A
17

NERD_tree comes with its own extension system; just put the following fragment into ~/.vim/nerdtree_plugin/yank_mapping.vim:

call NERDTreeAddKeyMap({
        \ 'key': 'yy',
        \ 'callback': 'NERDTreeYankCurrentNode',
        \ 'quickhelpText': 'put full path of current node into the default register' })

function! NERDTreeYankCurrentNode()
    let n = g:NERDTreeFileNode.GetSelected()
    if n != {}
        call setreg('"', n.path.str())
    endif
endfunction

Of course, you can adapt the default key (yy), and register ("; use + for the clipboard).

Adalbertoadalheid answered 4/5, 2013 at 19:57 Comment(4)
Great tip, Ingo! It works, but there's some way to copy the path from project root instead /home?Bollworm
@TárcioZemel: You mean, a path relative to the project root? You could transform it via the built-in fnamemodify() function (inside the above setreg() call).Adalbertoadalheid
Great, that's it! I just changed that line to call setreg('"', (fnamemodify(n.path.str(), ':.'))) and the magic happened! Thank you again!Bollworm
@TárcioZemel It seems that the parentheses around fnamemodify() is not necessary.Lafferty
L
6

This is what I found for NERDTree with a quick google: CopyPath

However it sounds like you are trying to make vim into Sublime Text. Vim tends to have a very different philosophy on text editing than most text editors. In my personal opinion it is often better to work with vim than against it. Here is a nice post by Drew Neil of Vimcasts explaining the benefit to split explorers.

Probably the more vim way of inserting a path is to use file completion. When in insert mode you can trigger this completion by pressing <c-x><c-f> then go through the menu with <c-p> and <c-n> (previous and next respectively). If you want to insert the current buffers path you can paste it via the % register e.g."%p or in insert/command-line mode press with <c-r>%.

For more help see:

:h ins_completion
:h i_CTRL-R
:h quote%
:h registers
Liquid answered 4/5, 2013 at 1:50 Comment(0)
B
5

I guess what you really need is a context menu like that sublime plugin?

That's built-in with NERDTree.

Just hit m on the node you highlighted and you'll see a new window pop under asking you what you want to do. The basic functions are: Add, Delete, Move, Copy.

There is also a plugin to let you search(using grep), either for single file or whole directory highlighted.

NERDTree also provides API for your easily built any custom actions in that context window.

Bannister answered 4/5, 2013 at 7:12 Comment(2)
There's also the (l) List option, which is probably what was asked aboutMurphey
Out of all the answers and comments, this comment by @Murphey is what I was looking for: an easy way to copy the full path to the selected file, which is the original question as far as I can tell too. Note that to copy the filepath returned by pressing m l depends on the terminal (sometimes hold shift and double click, sometimes drag over the filepath and ctrl+c, etc).Lioness

© 2022 - 2024 — McMap. All rights reserved.