enter image description here

Is there any way to stay always in the same netrw buffer after selecting a file to edit?


Netrw already includes code that attempts to reuse buffers but, as you have discovered, this code currently appears not to handle tree listings and ends up creating many buffers.

So the first thing to try is to make sure you are using the latest version of netrw. It looks like the code has changed as recently as July 2019, so it's possible this is something that has been fixed.

If that fails, however, and presuming you're not planning on altering the netrw plugin itself, your options include:

  1. Stop using g:netrw_liststyle = 3. In netrw's other list styles the buffers are named simply after the directory they display, and far fewer of them are created.
  2. Stop using netrw. It's possible that NERDTree or dirvish (which doesn't currently support tree views, but has them on the roadmap) don't exhibit this issue.
  3. Note that the buffers aren't listed in ls, so if you can refrain from using the :ls! version of the command maybe you can kid yourself that they don't exist at all.
  4. If option 3. is unpalatable, perhaps you can live with the fact that the buffers are created, but get them to remove themselves when they are hidden. This should be as simple as setting bufhidden=wipe for each netrw buffer with a simple autocmd FileType (as suggested on the linked page), but at least on my machine, netrw itself overwrites bufhidden, so some shenanigans may be required. This code worked for me:

    function! WipeBuffer(name, id)
      let buffer_number = bufnr(a:name)
      execute 'bwipeout' buffer_number
    augroup WipeNetrw
      autocmd BufHidden * if &ft == 'netrw' | call timer_start(100, function('WipeBuffer', [expand('<afile>')])) | endif
    augroup END
  • Can't believe I'm the only upvote on this solid, well researched answer.
    – B Layer
    Feb 5 '20 at 5:50
  • @BLayer I appreciate the sentiment. Thanks!
    – Rich
    Feb 5 '20 at 12:00
  • You're welcome. The voting patterns here in Vi/Vim SE baffle me sometimes, this being a glowing (glaring?) example, so I felt compelled to comment (maybe the activity will bump it into view of some voters...haha. :P )
    – B Layer
    Feb 5 '20 at 12:40

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.