How can I close the Netrw buffer?
I tried both :bd and :bw and neither work.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityThe 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
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.
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.
:close
?