doctopus
doctopus

Reputation: 5647

Close netrw explorer after opening a new file

I'd like the netrw explorer to automatically close after opening a file.

For example:

  1. :Lex to open file explorer
  2. Open file
  3. Upon open, the file explorer automatically closes.

I've tried setting:

autocmd FileType netrw setl bufhidden=wipe

But it doesn't work.

Rest of relevant .vimrc settings:

let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_altv = 1
let g:netrw_winsize = 25
noremap <C-x> :Lex<CR>
autocmd FileType netrw setl bufhidden=wipe

Upvotes: 3

Views: 1373

Answers (3)

Subfuzion
Subfuzion

Reputation: 1929

I also wanted this and couldn't find a solution (looking for one is how I stumbled here), so I rolled up my sleeves and came up with the following single-liner.

autocmd FileType netrw autocmd BufLeave <buffer> if &filetype == 'netrw' | :bd | endif

I know it seems redundant, but the test for the buffer filetype (netrw) is necessary. If there's a way to optimize this more, I'm sure a vim guru like Tim Pope can do it, but given that question is almost two years old now, hopefully this will help others who also stumble here.

Upvotes: 0

Emilio
Emilio

Reputation: 2752

I took this snippet from another Stack Overflow answer (linked below) and added it to the BufWinEnter event to solve this problem.

" Close after opening a file (which gets opened in another window):
let g:netrw_fastbrowse = 0
autocmd FileType netrw setl bufhidden=wipe
function! 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
augroup closeOnOpen
  autocmd!
  autocmd BufWinEnter * if getbufvar(winbufnr(winnr()), "&filetype") != "netrw"|call CloseNetrw()|endif
aug END

Original answer here: https://vi.stackexchange.com/questions/14622/how-can-i-close-the-netrw-buffer

This one in particular https://vi.stackexchange.com/a/29004

Upvotes: 4

user21497
user21497

Reputation: 1181

Try using :Explore, :Sexplore, :Hexplore, or :Vexplore instead of :Lexplore. The primary reason for :Lexplore is to have a persistent explorer on one side or the other; the others already go away when you select a file with <cr>.

Upvotes: -2

Related Questions