There are three methods for creating new files in the vi editor: two of them create new empty files, while the other creates a copy of an existing open file with a new name. This article assumes basic knowledge of vi modes and commands.
Open a terminal window and type the following at the command line:
The terminal screen will be replaced by the vi interface. At the top of the terminal window you will see the blinking cursor sitting above a column of ~ characters running down the side of the screen. At the bottom of the terminal window you should see:
"myFileName" [New File]
Edit the file (add some content) and when you are done switch to command mode and enter the following:
Vi writes the file to disk, and you'll know this because at the bottom of the window you will see:
"myFileName" [New] 1L, 4C written
The numbers, 1 and 4, will vary depending upon how many lines (L) and characters (C) vi wrote to disk.
Create a copy of your new file by executing the following in command mode:
At the bottom of the terminal window you will now see:
"copyOfFileName" [New] 1L, 4C written
However, contrary to what you may expect, vi still presents you with the buffer for "myFileName," not "copyOfFileName." The editor did create the copy and write it to disk, but it assumes that you intend to continue editing the original copy of the file. To open the copy execute the following:
If you are using the vim editor (vi improved) then you can skip this two step process by creating the copy this way:
Vim makes the copy, writes it to disk and switches the current buffer to the new file.
Open a new file for editing while still within the vi editor by executing the following:
A new, empty buffer appears and the following text appears at the bottom of the terminal window:
"aBrandNewFile" [New File]
The result of ":e newFile" called while using vi is almost identical to calling "vi newFile" from the command line. The difference is that by opening a new file within vi you may also continue to edit any other buffers that were open before.
To create multiple new files at once from the command line, simply add file name arguments to vi, such as: vi file1 file2 file3. The same cannot be done when creating new files from within vi using the ":e" command.
To list all currently open buffers in vi, enter the command ":ls". This displays a list of all of the buffers preceded by a number. To switch to another open buffer execute the command ":b1", replacing "1" with the number of the buffer you desire.