The vi (or vim) text editor is a very effective text editor for Linux / UNIX systems. It has been around since 1976 and you either love it or hate it. In order to be effective with the editor, it is important to know all the commands. One such command is specifying the name, or save as, of the file before you write it to disk. Let's take a look at how to use the "save as" feature in vim.

Most often the filename is specified when opening the vi editor, like so:

$ vim newfile.txt

Once your are done editing and write it to disk, this is the filename used. However, there are occasions where you want to save the file to a new name. Maybe you opened the editor from standard input? Maybe you just want to save the file under a new name and leave the original file unchanged? To specify the name of file when saving in vi, simply add it after the write command.

To specify the filename when saving in vi, first press escape to ensure you are out of insert mode. Then use the :w <filename> command.

:w <filename>

NOTE: You can also use :wq <filename> to quit the editor after saving.

Using the vi / vim save as feature

In the example above we opened a file named newfile.txt and saved it as harrison-bergeron.txt.

You can also specify the directory you want to save the file in. Just do the same as above, but give the full path to your desired file.

:wq /home/mcherisi/harrison-bergeron.txt

If you attempt to use a filename that already exists you will see an error message (see below in red).

The message explains how to overwrite a file using vim. If you are sure you want to overwrite the file, add an exclamation point to the end of the write command.

:wq! /home/mcherisi/harrison-bergeron.txt

Resources and Further Reading