How to save existing file to a new file (save as) in vi / vim

How to save existing file to a new file (save as) in vi / vim

To save an existing file to a new file using the vi or vim text editor, you can use the following steps:

  1. Open the file you want to save as a new file in vi or vim.

  2. Press the Esc key to exit insert mode.

  3. Type :w followed by the name of the new file you want to create, and press Enter. For example:

refer ‮:ot‬lautturi.com
:w newfile.txt

This will save the file as newfile.txt.

Alternatively, you can use the :saveas command to save the file as a new file. For example:

:saveas newfile.txt

This will save the file as newfile.txt.

Keep in mind that the :w and :saveas commands will overwrite the new file if it already exists. To save the file as a new file without overwriting an existing file, you can use the :saveas! command. For example:

:saveas! newfile.txt

This will save the file as newfile.txt, but will prompt you to confirm the operation if newfile.txt already exists.

You can also use the :w! command to save the file as a new file without overwriting an existing file. For example:

:w! newfile.txt

This will save the file as newfile.txt, but will prompt you to confirm the operation if newfile.txt already exists.

Keep in mind that you may need to use sudo to save the file if you do not have permission to write to the file or directory.

sudo :w newfile.txt
Created Time:2017-10-29 22:08:37  Author:lautturi