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.
Procedure to save existing file to a new file under vi or vim
- There is no “Save as” dialog in vim or vi text editor. However, one can run the following command to save existing file.
- Choose a filename that does not exist yet. For example, foo.txt
- Now press ESC key
- Type, :w foo.txt (where foo.txt is the filename you chose to save as in vi or vim)
Let us see all 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:
Press i to insert some text:
To save a file press
After saving your changes, you can quit Vim with (first press ESC):
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:
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.