2

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

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? – Niraj Gupta Jun 16 '16 at 4:06
  • Is it not working there? – SibiCoder Jun 16 '16 at 5:28
  • that does not seem to work reliably. – Christian Brabandt Jul 20 '16 at 7:22
1

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

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 Feb 27 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 Feb 28 at 3:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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