ChatGPT解决这个技术问题 Extra ChatGPT

How do you open a file from within Vim?

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.

You should really go through vimtutor. It covers all of the basic topics like this.

s
sashang

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


:e . also opens a file browser.
Thanks sashang for taking the time to answer. This is exactly what I was looking for.
Is there a way to return back to the first file ?
@thanos.a just type :b 1 and it should go to the 1st opened file.
@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.
K
Kent

this vim command you won't forget:

:Sex

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


: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)
W
William Pursell

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.


or you can open new tab with file via :tabnew <filepath>