47

My question is fairly simple, still I can't find the answer anywhere : In Vim, how can I close Netrw explorer without actually selecting a file? What keystroke should I hit to close the explorer and go back to the currently opened file?

So far, I have to select the currently opened file from within Netrw if I want to get back to it, which happens to be impossible if I haven't saved it yet.

I may add that I RTFM, and do know the :h netrw command ;)

Many thanks.

Alexandre Bourlier
  • 3,972
  • 4
  • 44
  • 76

6 Answers6

45

Use <c-6>/<c-^> to go back to the previous buffer. See :h CTRL-6.

Pro-tip: use :Rex to resume exploring. See :h :Rex

Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
  • 2
    Many thanks. On my machine, this diminishes the font size of my Terminal though. Probably some Gnome 3 settings, or Fedora's. Using `:b#` did the tricks of going back to the previous buffer ;) Many thanks ! – Alexandre Bourlier Dec 29 '14 at 16:35
  • 1
    After looking at the help entry, I had to do `` (i.e. ``) for this to work. (OSX Terminal.app) – Micah Smith Apr 07 '16 at 20:16
  • Is there a way to assign :Explore to F3, and then use the same key to close the explorer? Like NerdTreeToggle. – gianni Oct 05 '22 at 09:00
33

Vim by default, without plugins etc., should treat netrw windows like buffers (without listing it like one (:ls or :buffer). Try:

:bd
:bdelete

or

:bw
:bwipe

Peculiar way Vim did not list the netrw windows as buffer, but close it like one, and went back to former buffer/file.

Emil Asmussen
  • 431
  • 4
  • 3
  • 4
    Hmmm... actually, if I am using several windows, this will close the current windows I am using, which is bad... – Alexandre Bourlier Apr 29 '15 at 10:18
  • 1
    @AlexandreBourlier: then you don't want to close/quit netrw. You want something in its stead. You probably need to upgrade netrw to get :Rex working. Other than that, simply :e whatever-file-you-want. If you want a new but empty buffer, then use :enew . – user21497 Apr 29 '15 at 15:12
  • 5
    Thanks this is great !!! Also, actually, the buffer exists, but is hidden. Try `:ls!` and you will see it appearing in the list :) ... And `bd` with the opened netrw directory buffer number will do the trick as well, if for any reson you switched buffers and let some netrw buffers open. – Joel.O Sep 24 '16 at 14:24
  • 4
    Sorry it should be `:bd` in my precedent comment (cannot edit anymore) – Joel.O Sep 24 '16 at 14:32
  • 1
    As @Joel.O says, you may check which buffer is with `:buffers` and then delete it with `:bd` (I needed to use `:bw`) – Enrico Mar 21 '17 at 02:14
7

With v153 of netrw, one may use :Rex to return to the netrw buffer; it will also permit you to return to the file being edited prior to editing a directory. (see http://www.drchip.org/astronaut/vim/index.html#NETRW for the latest netrw).

user21497
  • 1,061
  • 8
  • 9
  • Thanks for this contribution; However I tried and it is not working for me. Entering `:Rex` from Netrw just doesn't do anything – Alexandre Bourlier Jan 24 '15 at 15:56
  • What version of netrw are you using? netrw v154b may be found on http://www.drchip.org/astronaut/vim/index.html#NETRW . – user21497 Apr 28 '15 at 14:42
  • 1
    @AlexandreBourlier is that because your previous buffer was a [NO NAME] ? :Rex only returns if you were in a named buffer, as far as I can tell. – shmup Jan 11 '18 at 01:18
1

Create the below function:

function! s:close_explorer_buffers()
    for i in range(1, bufnr('$'))
        if getbufvar(i, '&filetype') == "netrw"
            silent exe 'bdelete! ' . i
        endif
    endfor
endfunction

Add mapping:

nnoremap <C-e><C-x> :call <sid>close_explorer_buffers()<cr>
wisefool
  • 324
  • 2
  • 3
  • For the record, mapping this to `QuitPre` was what I needed to prevent a ``-collapsed `:Lexplore` from turning `:q :q` into `:q :q :q` in the case of `E173: ... more files to edit`. – ssokolow Nov 05 '19 at 09:11
0

I ran into this issue and followed Peter Rincker's suggestion of going to the alternate file.

I used the following remapping to make this easier

map <leader><leader>. <C-^>

Since my leader is ,, I have the keystrokes ,,. for alternate file, which leaves the netrw file explorer thing (which I don't know anything else about at this point).

evianpring
  • 3,316
  • 1
  • 25
  • 54
-1

I find that using :exit closes the netrw buffer and returns to the previous buffer.

thekyle
  • 41
  • 4