1

I'd like to add the file to the buffer list without changing the currently active netrw window. I'd also love to make this the default behaviour for netrw. Any advice? I've searched around and looked through the netrw help but can't find a solution.

statox
  • 49,782
  • 19
  • 148
  • 225
ergusto
  • 111
  • 3

1 Answers1

2

The netrw plugin provides functionality to set up user-specific mappings, cp. :help netrw-usermaps. Unfortunately, when I tried this, I found the mentioned netrw#Call() function defective.

I've sent the following patch to its author:

--- autoload/netrw.vim.orig 2016-11-18 14:28:34.647159594 +0100
+++ autoload/netrw.vim  2017-01-12 10:42:07.097512428 +0100
@@ -11013,11 +11015,7 @@ fun! netrw#Access(ilist)
" netrw#Call: allows user-specified mappings to call internal netrw functions {{{2
fun! netrw#Call(funcname,...)
"  call Dfunc("netrw#Call(funcname<".a:funcname.">,".string(a:000).")")
-  if a:0 > 0
-   exe "call s:".a:funcname."(".string(a:000).")"
-  else
-   exe "call s:".a:funcname."()"
-  endif
+  return call("s:".a:funcname, a:000)
"  call Dret("netrw#Call")
endfun

With that fix, I was able to implement a + user mapping to do what you've asked for:

" +                     Add the current file to the buffer list, and go to the
"                       next file entry.
function! NetrwBufAdd( isLocal )
    let l:filespec = netrw#Call('NetrwFile', netrw#Call('NetrwGetWord'))
    if filereadable(l:filespec)
        execute 'badd' ingo#compat#fnameescape(l:filespec)
    endif
    execute 'normal!' (b:netrw_liststyle == netrw#Expose('WIDELIST') ? 'W' : 'j')
    return ''
endfunction
let g:Netrw_UserMaps = [
\   ['+', 'NetrwBufAdd']
\]

Overriding the default behavior should be a simple addition of ['<CR>', 'NetrwBufAdd'] to the above list.

Ingo Karkat
  • 17,819
  • 1
  • 45
  • 61
  • (the patch is merged now.) Is there any difference between using Netrw_UserMaps and ~/.vim/after/ftplugin/netrw.vim (as seen in https://vi.stackexchange.com/questions/4552/how-can-i-move-netrws-default-keybindings-to-other-keys ? – user202729 Sep 21 '21 at 08:15
  • @user202729 The filetype location uses the fact that the plugin sets the filetype of its scratch buffer to netrw, whereas the config variable is a custom extension point provided by the plugin itself. In practice, it likely doesn't matter much which one you use. – Ingo Karkat Sep 21 '21 at 19:34