How to Create a File in VI Editor

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

Open a terminal window and type the following at the command line:

Step

vi myFileName

Step

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:

Step

“myFileName” [New File]

Step

Edit the file (add some content) and when you are done switch to command mode and enter the following:

Step

:w

Step

Vi writes the file to disk, and you’ll know this because at the bottom of the window you will see:

Step

“myFileName” [New] 1L, 4C written

Step

The numbers, 1 and 4, will vary depending upon how many lines (L) and characters (C) vi wrote to disk.

Step

Create a copy of your new file by executing the following in command mode:

Step

:w myCopyOfFileName

Step

At the bottom of the terminal window you will now see:

Step

“copyOfFileName” [New] 1L, 4C written

Step

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:

Step

:e copyOfFileName

Step

If you are using the vim editor (vi improved) then you can skip this two step process by creating the copy this way:

Step

:sav myCopyOfFileName

Step

Vim makes the copy, writes it to disk and switches the current buffer to the new file.

Step

Open a new file for editing while still within the vi editor by executing the following:

Step

:e aBrandNewFile

Step

A new, empty buffer appears and the following text appears at the bottom of the terminal window:

Step

“aBrandNewFile” [New File]

Step

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.