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

Is it possible to save existing file directly when using vi or vim text editor? How do I run “save as” command in vi/vim running on a Linux or Unix-like systems?

Vi is a text editor for Linux and Unix-like systems. An updated version of vi shipped with modern Unix system called vim (Vi IMproved) with upwards compatible to Vi. One can use vi to edit any plain text file. However, vim is very useful for editing programs.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements vi or vim running on Linux or Unix-like systems
Est. reading time 2m

Procedure to save existing file to a new file under vi or vim

  1. There is no “Save as” dialog in vim or vi text editor. However, one can run the following command to save existing file.
  2. Choose a filename that does not exist yet. For example, foo.txt
  3. Now press ESC key
  4. Type, :w foo.txt (where foo.txt is the filename you chose to save as in vi or vim)

Let us see all vim / vi command in details.

vim save as command

Let us start vi/vim and create a new file named bar.txt. Open the terminal application and type the following command:
vim bar.txt
Press i to insert some text:

To save a file press ESC and :w. The last command will save the current file. Now let us say you want to save “bar.txt” as “foo.txt” using vim. The command syntax is as follows:
:w FILENAME
:x FILENAME
:x foo.txt


After saving your changes, you can quit Vim with (first press ESC):
:q

How do I save a file in other directory in vim/vi?

You must type the full path to the location where you want to save the file with the write command. For instance, save file in /home/vivek/backups/foo.txt:
:w /home/vivek/backups/foo.txt

Saving a file in Vim / Vi without root permission with sudo trick

Let us open a file called /etc/httpd/cyberciti.biz.conf:
$ vim /etc/httpd/cyberciti.biz.conf
To save /etc/httpd/cyberciti.biz.conf as “/etc/httpd/cyberciti.com.conf” in vim or vi as root user, type:
:w !sudo tee /etc/httpd/cyberciti.com.conf

Conclusion

Please note that if you are not editing an existing file, you can always provide a file name when you save. You can do this with :w filename. The same command act as “save as” command too.


🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one


CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum