21

How can I close the Netrw buffer?

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

1
  • 2
    It's in its own window/pane? :close?
    – B Layer
    Dec 22, 2017 at 2:16

4 Answers 4

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

1
  • Using both (autocmd and fastbrow) worked for me Dec 21, 2022 at 3:47
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.

-3

typing q while in the buffer closes mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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