How to Save a File in Vim or Vi and Quit the Editor
Save a File in Vim or Vi and Quit the Editor
Vim or Vi is the popular text editor among the Linux System Administrators and Software Developers. Vim comes preinstalled on major Linux distributions and macOS. The Vim text editor is used to edit configuration files or program files. In this tutorial, you are going to learn how to Save a File in Vim or Vi and Quit the Editor.
Vim / Vi Modes
There are two modes in Vim text editor which are given below:
After launching Vim each time you are in Normal Mode. In the Normal Mode, you can navigate through the file and can execute Vim commands.
While you can launch Insert Mode by pressing i
when you are in Normal Mode. In Insert Mode, you can insert or delete characters as in other text editors. To go back to normal Mode just press Esc
key.
How to open/create a file in Vim or Vi
It’s very easy to open a file in Vim and edit it. To open a file just type Vim
followed by the filename you want to create or edit.
The basic syntax for opening a file in Vim text editor is given below:
vim [options] filename.txt
You can run below command to open demofile.txt file in Vim editor:
vim demofile.txt
The output should look like:
How to Save a file in Vim or Vi
You can save a file in Vim or Vi by pressing :w
followed by Enter
key without quitting the file. Now follow the below steps to save the file:
- If you are in
INSERT MODE
then pressEsc
key to come inNORMAL MODE
- Then type
:w
to write the changes to the file. - Finally hit
Enter
to submit all the changes.
How to Save and Quit a file in Vim or Vi
You can save and quit a file in Vim or Vi text editor by pressing :wq
followed by Enter
key. Now follow the below steps to save and quit the file:
- If you are in
INSERT MODE
then pressEsc
key to come inNORMAL MODE
- Then type
:wq
to write the changes to the file. - Finally hit
Enter
to submit all the changes and Quit Vim.
How to Quit Vim by trashing all changes
You can quit Vim or Vi text editor without saving changes by pressing :q
followed by Enter
key. Now follow the below steps to save and quit the file:
- If you are in
INSERT MODE
then pressEsc
key to come inNORMAL MODE
- Then type
:q
to write the changes to the file. - Finally hit
Enter
to submit all the changes and Quit Vim.
You can learn more about Vim commands by typing following command in the terminal:
vimtutor
To launch vimtutor
in the French language run below command:
vimtutor fr
Conclusion
In this tutorial, you have learned How to Save a File in Vim or Vi and Quit the Editor. If You have any queries regarding this please comment below.
Originally published at https://linux4one.com on December 9, 2019.