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:
- 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.
- 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.
- 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.
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=wipefor each netrw buffer with a simple
autocmd FileType(as suggested on the linked page), but at least on my machine,
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 endfunction augroup WipeNetrw autocmd! autocmd BufHidden * if &ft == 'netrw' | call timer_start(100, function('WipeBuffer', [expand('<afile>')])) | endif augroup END