2

Is it possible to copy the absolute filepath of the file under the cursor in Netrw?

3 Answers 3

3

You can select the file under cursor through visual block mode, like Ctrl+V, then press $ and then a j and then a y.

Now, in command line mode, Type

:echo @%.@"

This will show the full path of the file under cursor.

You can also map it to a key, Like

:map \f ^<C-V>$jy:echo @%.@"<CR>

Now, you can press \f while the cursor is on a file and it will print it's full path in command line.

3
  • What if I am in TreeListing mode? Commented Jun 16, 2016 at 4:06
  • Is it not working there?
    – SibiCoder
    Commented Jun 16, 2016 at 5:28
  • that does not seem to work reliably. Commented Jul 20, 2016 at 7:22
2

This does not seem to be possible out of the box. Here is a small patch, that should do what you want:

diff --git a/runtime/autoload/netrw.vim b/runtime/autoload/netrw.vim
index de85844..c18d85d 100644
--- a/runtime/autoload/netrw.vim
+++ b/runtime/autoload/netrw.vim
@@ -6052,6 +6052,7 @@ fun! s:NetrwMaps(islocal)
nnoremap <buffer> <silent> <nowait> v       :call <SID>NetrwSplit(5)<cr>
nnoremap <buffer> <silent> <nowait> x       :<c-u>call netrw#BrowseX(<SID>NetrwBrowseChgDir(1,<SID>NetrwGetWord(),0),0)"<cr>
nnoremap <buffer> <silent> <nowait> X       :<c-u>call <SID>NetrwLocalExecute(expand("<cword>"))"<cr>
+   nnoremap <buffer> <silent> <nowait> _        :call setreg(v:register, get(b:, 'current_dir', getcwd()). '/'. <SID>NetrwGetWord())<cr>
"   " local insert-mode maps
"   inoremap <buffer> <silent> <nowait> a      <c-o>:call <SID>NetrwHide(1)<cr>
"   inoremap <buffer> <silent> <nowait> c      <c-o>:exe "NetrwKeepj lcd ".fnameescape(b:netrw_curdir)<cr>

Using this, you can copy a file path using the _ key

Update (2021):
With netrw v170 you can also do this:

:echo getcwd() .. (&shellslash ? '/' : '\') .. netrw#Call("NetrwGetWord")

You can of course map it to e.g. return the result directly into a register.

4
  • I think it's possible now (echo netrw#Call("NetrwGetWord")), you may want to update the answer?
    – user202729
    Commented Sep 21, 2021 at 8:12
  • @user202729 With what netrw version does that work? Commented Sep 21, 2021 at 10:38
  • (I mean that one just echo the selected file, you need extra logic to copy it) but I'm using v170.
    – user202729
    Commented Sep 22, 2021 at 0:34
  • Well, it's just slightly more comfortable. You still need to prefix it with the current directory. But I have just added it to the answer Commented Sep 22, 2021 at 6:35
0

Another option is modifying the netrw.vim script found in /usr/share/vim/vim<your version here>/autoload/ as follows:

nnoremap <buffer> <silent> P     : call NetrwGetPath()

function! NetrwGetPath()

    let dirname = s:NetrwTreeDir(0)                                                                  
    let filename = s:NetrwGetWord()       

    " this global variable will store the path that you want copied from netrw                 
    let g:netrwtemppath = dirname."/".filename

endfunction 

where P can be substituted for any key you want to remap for this. Then all you need to do to paste into a file is:

:put =netrwtemppath
2
  • Welcome to Vi and Vim! Why modify the shipped netrw.vim instead of extending it through a separate file? One issue with overwriting it is that updating the Vim package will undo your modifications...
    – filbranden
    Commented Feb 27, 2021 at 1:31
  • 1
    I agree, that's a definite flaw of my answer. I just didn't understand the other answer and wanted to try something simple that I knew how it worked. My function relies only on two built in functions. I suppose I could have written all that in a separate file and then just made the two built in functions accessible to my script, but that's still a modification of the shipped netrw.vim. Would love any suggestions for making my function in a separate script without modifying netrw.vim.
    – wxz
    Commented Feb 28, 2021 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.