Vim - Quit/Close Netrw without selecting any file
Asked Answered
H

6

51

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.

Hadfield answered 29/12, 2014 at 15:39 Comment(0)
I
47

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

Ingeminate answered 29/12, 2014 at 16:5 Comment(3)
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 !Hadfield
After looking at the help entry, I had to do <c-^> (i.e. <c-S-6>) for this to work. (OSX Terminal.app)Erection
Is there a way to assign :Explore to F3, and then use the same key to close the explorer? Like NerdTreeToggle.Breedlove
D
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.

Descent answered 25/4, 2015 at 16:54 Comment(5)
Hmmm... actually, if I am using several windows, this will close the current windows I am using, which is bad...Hadfield
@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 .Natashianatassia
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<number> 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.Putup
Sorry it should be :bd<number> in my precedent comment (cannot edit anymore)Putup
As @Putup says, you may check which buffer is with :buffers and then delete it with :bd<number> (I needed to use :bw)Surveying
N
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).

Natashianatassia answered 23/1, 2015 at 20:22 Comment(3)
Thanks for this contribution; However I tried and it is not working for me. Entering :Rex from Netrw just doesn't do anythingHadfield
What version of netrw are you using? netrw v154b may be found on drchip.org/astronaut/vim/index.html#NETRW .Natashianatassia
@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.Babara
C
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>
Choker answered 26/9, 2019 at 6:32 Comment(1)
For the record, mapping this to QuitPre was what I needed to prevent a <C-Tab>-collapsed :Lexplore from turning :q :q into :q :q :q in the case of E173: ... more files to edit.Maurili
C
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).

Couthie answered 10/1, 2023 at 9:14 Comment(0)
E
-1

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

Eldwon answered 18/6, 2020 at 14:4 Comment(1)
This is totally wrong. It closes the window/split, if there's only one window, it quits Vim.Potiche

© 2022 - 2024 — McMap. All rights reserved.