6

I would like to automatically close the netrw window when I open a file. I've found a similar question (NERDTree How to open a file and automatically close the explorer buffer), but it refers to NERDTree - not netrw.

This is my .vimrc file:

let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_browse_split = 4
let g:netrw_altv = 1
let g:netrw_winsize = 25
set autochdir
map <C-E> :Lexplore<CR>

Edit: This works when using :Explore, but in my case, I'm trying to make it work when I use :Lexplore

Gilad Novik
  • 161
  • 1
  • 4
  • 3
    Isn't that what happens by default? E.g. I run :Explore, the netrw Directory Listing comes up in the current buffer, I select a file pressing <Enter> and that file is opened in the current window (and netrw is closed)? – Christian Brabandt Jul 19 '17 at 07:28
  • @ChristianBrabandt when you use :Explore - yes, but I'm using :Lexplore (I don't like when it covers everything) – Gilad Novik Jul 19 '17 at 20:19
  • That is an information, that you should have shared in your question. – Christian Brabandt Jul 20 '17 at 05:28
  • 2
    @ChristianBrabandt My apologies, I assumed it would be obvious from my .vimrc file. – Gilad Novik Jul 20 '17 at 05:52
  • Did you find a solution @GiladNovik? –  Mar 19 '18 at 03:35
  • As indicated in another comment, :Lex is a persistent browser (stays open until manually closed): https://stackoverflow.com/questions/61766814/close-netrw-explorer-after-opening-a-new-file ... I have this in my ~/.vimrc : noremap <silent> <leader>l :Lex<CR> ("leader elle") – Victoria Stuart Mar 08 '21 at 18:26

1 Answers1

6

I think your problem comes from the following line of your .vimrc:

let g:netrw_browse_split = 4

From the doc :h g:netrw_browse_split you can read:

  *g:netrw_browse_split*    when browsing, <cr> will open the file by:
                =0: re-using the same window  (default)
                =1: horizontally splitting the window first
                =2: vertically   splitting the window first
                =3: open file in new tab
                =4: act like "P" (ie. open previous window)

So this should solve your problem:

let g:netrw_browse_split = 0

Otherwise I think you may be able to adapt the autocommand solution from the question you linked since netrw buffers has a filetype set to netrw.

statox
  • 49,782
  • 19
  • 148
  • 225
  • 1
    I've just tried to change it to 0 - it still opens in the same window, but netrw still stays open after. – Gilad Novik Jul 19 '17 at 20:17
  • 1
    @GiladNovik I just read your comment answering to ChristianBrabandt, the doc also says This option does not affect :Lexplore windows. so indeed that might not be a solution for you. – statox Jul 20 '17 at 07:31
  • I'm unsure what the OP means but if you want to close netrw if it's the last buffer, e.g. when you are exiting this file you opened, then see https://stackoverflow.com/questions/29012106/automatically-quit-vim-if-netrw-is-last-and-only-buffer – Colin D May 28 '19 at 20:36