Vim is a useful command-line text editor. As a featureful text editor, Vim/Vi provides different ways and commands in order to save a file. In this tutorial, we examine how to save the file with the same name or a different name or exit after saving the file.
Save File with write Command
The write command is used to save or write changes into the current file. In order to use the write command first, the normal mode should be enabled.
- Press ESC key to change normal mode.
- Type :write in order to save file and then press Enter.
:write
The Vim uses the shortcuts regularly and the :w can be used as a shortcut for the :write command.
- Press ESC key to change normal mode.
- Type :w in order to save file and then press Enter.
:w
When the write or save operation is completed some information will be provided about save under Vim.
- sample.txt is the file name which is saved.
- 17L is the line numbers which are saved.
- 3122C is the characters count which are saved.
Save File with Different Name
By default, the save operation will save the changes into the current file with the same file name. But you can save the changes into a new file and continue with this file by providing the file name to the :write
or :w commands. In the following example, we will save the file as a new file with the name mysample.txt.
- Press ESC key to change normal mode.
- Type :write mysample.txt in order to save file and then press Enter.
:write mysample.txt
Or as a short form.
- Press ESC key to change normal mode.
- Type :w mysample.txt in order to save file and then press Enter.
:w mysample.txt
Save File and Quit
Vim provides different commands in order to save files and exit. This is very useful for fast operation. We will provide the wq commands which are short forms of the write and quit.
- Press ESC key to change normal mode.
- Type :wq in order to save file and then press Enter.
:wq
An alternative way is using the x command according to the wq command.
- Press ESC key to change normal mode.
- Type 😡 in order to save file and then press Enter.
:x
Quit Without Saving File
You can make different changes in a file and then want to not save these changes and exit from the file without saving changes. You can use the quit! or q! commands which will quit or exit from the Vim without saving changes.
- Press ESC key to change normal mode.
- Type :quit! in order to save file and then press Enter.
:quit!
Or
- Press ESC key to change normal mode.
- Type :q! in order to save file and then press Enter.
:q!