177

When I open a directory browsing in vim, I can open a file by moving the cursor to a file name and pressing Enter.

Now, is there any command to go back to the previous directory browsing?

If not, is there a way to open a file without closing the directory browsing mode?

slhck
  • 36,575
  • 28
  • 148
  • 201
MKo
  • 4,768
  • 8
  • 32
  • 35

7 Answers7

277

You can go back to the last buffer using :b#.

If you just opened a file, then it will bring you just back to the directory browsing.

Update: Since this answer happened to be accept as the correct answer and is thus on the top, I'd like to summarize a bit the answers, including the one by @romainl that imho is the correct one.

claymation
  • 2,475
  • 4
  • 27
  • 36
ezdazuzena
  • 6,120
  • 6
  • 41
  • 71
  • 3
    That should be `:b#`. I'd fix it, but single-character edits don't fly. – echristopherson Sep 07 '12 at 21:10
  • If I do `vim ` and then navigate to the file and press `Enter`, it does not work; if i open vim, then `:e `, select file and `Enter`, it works. In both cases `:buffers` shows only one buffer. I'm not sure why it is so. – mcmlxxxvi Feb 05 '16 at 15:45
  • @mcmlxxxvi according to the vim wiki `b#` only works for "previously edited buffers": http://vim.wikia.com/wiki/Easier_buffer_switching?action=edit&section=6 – ezdazuzena Feb 08 '16 at 11:18
  • How about on a mac? – Daniel Gelling May 16 '17 at 04:08
  • @DanielGelling sorry for the late answer. But yes, should be the same on Mac – ezdazuzena Mar 14 '18 at 09:01
  • 3
    Nice thing summarizing all the other correct answers, I wish more people do that. – LightMan Jul 19 '18 at 08:57
  • @ezdazuzena please add `Ctrl`-`i` for jumping forward so it can be used really nice with `Ctrl`-`o` for switching between the files – veich Jun 07 '20 at 04:54
  • 1
    @veich Thanks for your comment, but I won't do that for now since the question is only about going backward. This is not a wiki page, I'm just summarizing the other questions for the sake of clarity. Hope you understand that. – ezdazuzena Jun 08 '20 at 05:58
103

You can use Ctrl-O to go back to the previous buffer.

slhck
  • 36,575
  • 28
  • 148
  • 201
Peyman
  • 1,031
  • 1
  • 6
  • 3
  • 2
    Very useful in general (e.g. for navigating back from `gf`), but in case of exploring directories it doesn't bring me back to the "line" I was on when opening the file. `:Rex`, which is somewhat clumsy (5 keystrokes vs 1), does it. Probably this is a candidate for mapping... – Tomasz Gandor Jan 08 '19 at 21:13
51

The proper command for returning to the netrw listing is :Rex[plore]. You can run it even after you have jumped to and from dozens of files.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 1
    When editing a file after leaving the netrw listing, is returning with `:Rexplore` any different from returning with `:Explore`? – Jasha Jun 09 '20 at 16:14
  • 1
    To answer my own question, it appears that `:Rexplore` always returns you to the same netrw listing that you were browsing before, whereas `:Explore` gives the listing for the directory of whatever file is open currently. – Jasha Jun 09 '20 at 16:19
37

:Ex will take you back (and show you what you edited and didn't save).

drug_user841417
  • 797
  • 1
  • 7
  • 12
  • 1
    I upvoted EVERY one of the answers. they were all useful – WestCoastProjects Jul 20 '15 at 17:42
  • Nice, it is a bit better than `Ctrl-O` because it highlights the parent directory (`..`), instead of the top line (some Netrw header), but it's still not where I left the explorer (the opened file). 4 keystrokes vs 1, some saving of `j`s to navigate down to where I was, but still I am likely to map `:Rex` if I'm expecting a lot of navigation. – Tomasz Gandor Jan 08 '19 at 21:17
32

I rely on Ctrl ^ to go back and forth b/w two buffers.

Aman Jain
  • 10,927
  • 15
  • 50
  • 63
  • 1
    This worked for me with vim 7.3 on OSX from homebrew. Ctrl-O does not seem to work. – Gardner Bickford Mar 25 '14 at 21:09
  • 7
    You don't even need shift, can just Ctrl 6 – mVChr Apr 22 '15 at 06:57
  • more correctly, this "edits the alternate file": http://vimdoc.sourceforge.net/htmldoc/editing.html#CTRL-^ – ezdazuzena Mar 14 '18 at 09:04
  • Excellent! Albeit it just works back and forth (like a "1 level Undo"), but it actually returns to the line I was on! Looks like I'm not mapping `:Rex` after all! (Of course, only when I don't look forward to "go back" from nested navigations (jumped to another file etc.). – Tomasz Gandor Jan 08 '19 at 21:21
  • On a MacBook, it is CTRL + SHIFT + 6, not just Ctrl + 6 – aspiring1 Oct 27 '20 at 02:01
26

I usually just use :E. It's easy to remember and gets you back to the directory listing of the file you had open.

NM Pennypacker
  • 6,704
  • 11
  • 36
  • 38
  • 2
    I'm surprised this has too few votes. It's definitely an answer. – TaoPR Aug 21 '18 at 09:13
  • 2
    Don't be surprised - on my current vim 7.4.x `:E` is ambiguous and does nothing. You need to type a least `:E`. – Tomasz Gandor Jan 08 '19 at 21:19
  • 1
    This is the same as `:Ex` or `:Explorer` if not other command starting with `E` is defined. See for instance https://stackoverflow.com/a/14367507/1141095 – ezdazuzena Jun 05 '20 at 05:44
4

I'd like to use the Jump-motions shortcut keys to do any 'go back' or 'go forward' actions in Vim. It makes vim like a web browser.

You can search for Vim's help for more details.

Vim :h jump-motions

So like as mentioned in other answers ctrl+O works cool, and hope vim's help-file let you learn more commands like ctrl+I .

Ctrl + I : Go Forward

Ctrl + O : Go Back

chenlola
  • 71
  • 4