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.
Step 1
Open a terminal window and type the following at the command line:
Video of the Day
vi myFileName
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:
:w
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.
Step 2
Create a copy of your new file by executing the following in command mode:
:w myCopyOfFileName
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:
:e copyOfFileName
If you are using the vim editor (vi improved) then you can skip this two step process by creating the copy this way:
:sav myCopyOfFileName
Vim makes the copy, writes it to disk and switches the current buffer to the new file.
Step 3
Open a new file for editing while still within the vi editor by executing the following:
:e aBrandNewFile
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.
Video of the Day