19

Is there a built-in way to make Vim open a new buffer with file?

I want open a file. :enew doesn't take any arguments and opens a blank buffer. :edit closes my existing buffer.


Related (about :enew):

2 Answers 2

32

I think you might be misunderstanding what you're seeing happen. :edit does not close your current buffer. It just replaces it in the current window. If you type :buffers your previous buffer should still be listed. It is still open and in Vim's memory. The only way to make them go away is to run :bd or :bw (or :q of course).

So, to answer your question: :e newfile.txt

Your previous buffer is now the alternate file to the current buffer (:help alternate-file). You can switch back to it using ctrl-^ or ctrl-6.

There are commands similar to :edit

  • :split - horizontal split
  • :vsplit - vertical split

Both of these commands (shortened :sp and :vs respectively) take an optional file argument like :edit does. So, if you wanted to vertically split and create a new file in one command, you would type: :vs newfile.txt

Give :help edit-intro a read to get an idea of how Vim handles files, and :help windows-intro to read up on windows, buffers, and tabs.

10

I can accomplish the behavior using this, but it's kinda annoying to have to do a few extra keystrokes:

:enew         " shorthand: ene
:edit <file>  " shorthand: e

" Shorthand to do both
:ene|e <file>
1
  • @JairLopez This is opens a new split for me. From :h new: "This behaves like a ":split" first, and then an ":enew" command."
    – user4779
    Jun 21, 2016 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy