22

How can I close the Netrw buffer?

I tried both :bd and :bw and neither work.

Tyler Durden
  • 2,091
  • 2
  • 23
  • 42

4 Answers4

13

The autocmd FileType netrw setl bufhidden=wipe still leaves one buffer open. I found this solution, which closes it after opening the file:

let g:netrw_fastbrowse = 0

over at the discussion on https://github.com/tpope/vim-vinegar/issues/13

Brian C. Lane
  • 231
  • 2
  • 4
8

I've had this problem, when using Vinegar to make netrw easier to use. I found a site that offers one solution here, and more solutions here.

The solution that worked for me, was to add this to my .vimrc:

autocmd FileType netrw setl bufhidden=wipe

For the sake of completeness, other solutions:

@serebrov on Github:

function! QuitNetrw()
  for i in range(1, bufnr($))
    if buflisted(i)
      if getbufvar(i, '&filetype') == "netrw"
        silent exe 'bwipeout ' . i
      endif
    endif
  endfor
endfunction

autocmd MyAutoCmd VimLeavePre *  call QuitNetrw()

@slashfoo on Github:

" Remove 'set hidden'
set nohidden

augroup netrw_buf_hidden_fix
    autocmd!

    " Set all non-netrw buffers to bufhidden=hide
    autocmd BufWinEnter *
                \  if &ft != 'netrw'
                \|     set bufhidden=hide
                \| endif

augroup end

The first solution was posted by @tpope, also on github.

1

Tried all kind of solutions from here and some other places, none worked as expected, respectively I didn't like the implementation much (e.g. the one using augroup netrw_buf_hidden_fix), so I ended up with this for closing netrw using Esc or q

autocmd FileType netrw nnoremap <buffer><silent> <Esc> :call <SID>CloseNetrw()<CR>
autocmd FileType netrw nnoremap <buffer><silent> q     :call <SID>CloseNetrw()<CR>
...
function! s:CloseNetrw() abort
  for bufn in range(1, bufnr('$'))
    if bufexists(bufn) && getbufvar(bufn, '&filetype') ==# 'netrw'
      silent! execute 'bwipeout ' . bufn
      if getline(2) =~# '^" Netrw '
        silent! bwipeout
      endif
      return
    endif
  endfor
endfunction

Please note, always place autocmd inside a group.

Marius Burz
  • 111
  • 2
-3

typing q while in the buffer closes mine.

alpha_989
  • 901
  • 7
  • 15