Wondering how VI save and exit works? If your answer is yes, then I am here to help you out. There is no doubt that Vim is one of the best text editing tools available out there.
But the thing is that it has several modes of operation. As a result, the Vim text editor is extremely confusing to the new users.
However, if you are spending or going to spend a lot of your time in the terminal only, then learning how to use Vim text editor would be pretty helpful. But in this article, I will specifically be going to talk about how to save a file in Vim editor and exit it.
So here we go:
How to Save a File in Vim and Exit It? (VI save and exit Explained)
To start using Vim editor efficiently, you will need to get familiar with Vim modes. However, to help you to get started, whenever you launch Vim editor, you are in the normal mode.
On the normal mode, you can use different vim commands and navigate through the files. Also, to type a text, you have to insert it into the typing mode by pressing the “i” key.
Once you are in this mode, you can easily insert and delete texts or characters just like any other text editor. Also, to exit the mode, you have to press the Esc key.
However, to know more about the vim modes, I would recommend you checking out the Vim Editor Modes Explained article by Freecodecamp.
How to Open a File in Vim or Vi?
Opening a file using Vim editor is pretty easy. However, to make it easy for you, here are all the steps that you have to follow:
1. At first, launch Terminal on your Linux machine.
2. Then type down Vim followed by the name of the file you wish to edit or create.
3. Now, if you wish to edit the file, then press “i” and get into the edit mode.
4. To save the file, you have to press ESC and then type down the following command and hit enter.
5. To exit Vim, you have to type the following command and hit enter.
Apart from this, you can also try out another command which will help you to Vi save and exit the editor at the same time. For this, run the below command hit enter button:
Summary of vim/vi commands
|Press the ESC key, type :q! and hit enter button.||This will exit Vim editor without making any changes. And discard all the changes you have made.|
|Press the ESC key, type :wq and hit enter.||This will save a file and Exit Vim.|
|Press the ESC button and type :X followed by an Enter.||Save a File and Exit.|
How to exit Vim editor without making any changes?
You may also want to exit Vim without making any changes. For this, you will need to follow the below steps:
1. At first, press the ESC button to exit the edit mode.
2. Then run the following command to exit Vim without making any changes:
Get Familiar With Vim Commands
If you are new to the Vim editor, then I would recommend you to start the Vim tutor before going any further. For this, you will need to run the below commands one by one:
So that was all for the Vi save and exit. Vim editor is not as complicated as it seems. You just need to be familiar with few Vim commands, which are pretty simple to learn.
Anyway, now go ahead and try creating a vim editor by yourself and try to exit from there. Also, if there is anything you wish to ask, then do feel free to drop a comment below.