97

I know how to open a file in Vim from a terminal (vim fileName). What I can't figure out is how to open a file when I'm already within Vim.

I tried :r fileName, but that appears to read (or append) the file into the unsaved buffer I have open. It creates a new file, because when I try to write it with :w, it asks for a filename.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
jshock
  • 1,373
  • 2
  • 11
  • 17
  • 2
    You should really go through `vimtutor`. It covers all of the basic topics like this. – Max May 15 '14 at 14:17

3 Answers3

150

:e <filename>

or

:Ex <directory>

lets you browse for the file from the given directory.

:Ex on its own will open the pwd.

:enew

will create an empty buffer.

sashang
  • 11,704
  • 6
  • 44
  • 58
  • 15
    `:e .` also opens a file browser. – Max May 15 '14 at 14:15
  • Thanks sashang for taking the time to answer. This is exactly what I was looking for. – jshock May 15 '14 at 14:21
  • Is there a way to return back to the first file ? – thanos.a Nov 04 '19 at 11:55
  • @thanos.a just type :b 1 and it should go to the 1st opened file. – sashang Nov 06 '19 at 23:04
  • @sashang This doesn't work in my case. I open a file with vim a and then open a second one with :e b . I am trying :b 1 but doesn't seem to go back to file a. Using IM - Vi IMproved, version 7.4.1099 by Bram Moolenaar et al. – thanos.a Nov 07 '19 at 07:50
  • 1
    @thanos.a only works if you open from the cmdline. e.g. vim file1 file2. If you already have open buffers then the numbering will be different. If you just want to swap between files use ctrl-6 or :b#. eg :e file1 :e file2 :b#. now u should be back at file1. – sashang Nov 07 '19 at 09:26
57

this vim command you won't forget:

:Sex

if you want to point to certain dir, then :Sex <dir>

Kent
  • 189,393
  • 32
  • 233
  • 301
  • 9
    `:help Sex` turns out that it's `:Sexplore` command, "Split & Explore current file's directory", similarly there are `:Hex` (Horizontal Split and Explore) `:Vex` (Vertical) `:Tex` (newtab) – Weekend Jan 23 '19 at 03:47
  • And you can close the split using `CTRL+w+o` command. This executes the `:only` command and closes all other splits except the current file. – matrix Jun 18 '23 at 18:06
12

Also, to open multiple files (or just one, so I tend to use this for opening a single file, since :e fails to open multiple files)

:n file1 file2

:n resets the argument list so it is as if you had entered them on the command line (so commands like :rew will work with this list), but :e does not.

William Pursell
  • 204,365
  • 48
  • 270
  • 300