How do you open a file from within Vim?

VimVi

Vim Problem Overview


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.

Vim Solutions


Solution 1 - Vim

: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.

Solution 2 - Vim

this vim command you won't forget:

:Sex

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

Solution 3 - Vim

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjshockView Question on Stackoverflow
Solution 1 - VimsashangView Answer on Stackoverflow
Solution 2 - VimKentView Answer on Stackoverflow
Solution 3 - VimWilliam PursellView Answer on Stackoverflow