Vim is by far one of the most popular text editors on Linux systems, especially those who prefer command-line interfaces or spend a lot of time buried in terminals and command prompts.
However, Vim is far from basic and even features several different modes that can control the editor differently, making it a little bit intimidating for absolute beginners. In this article, we’re going over how you can save and exit your files in Vim.
Understanding Vim modes
Before we get to saving and quitting our files, we need to understand Vim modes. You see, the commands you need to enter for exiting Vim can only be entered in a certain mode which is, of course, different from the one you used to type in text in your file in the first place.
There are two main modes in Vim.
- Command Mode (default)
- Insert Mode.
As you can probably guess, Command mode is used to give commands to the editor about what to do with the file and navigate the file itself. The Insert Mode is used to insert text into the loaded file.
You can press the i key to enter the Insert mode. Press the Esc key in any mode to get back to Command mode.
Also read: How to use Journalctl to read Linux logs?
Vim: Saving and exiting
Before we get to saving changes in a file and exiting Vim, let’s look at how you can open files in Vim. All you have to do is use the vim command followed by the file name.
Alternatively, you can start Vim by typing in vim in the terminal and then use the :e command followed by the file name to open a file.
How to save a file in Vim?
Now to save a file once you’re done making changes, press the Esc key on your keyboard to go back to command mode. Now type :w and press enter to save your file.
You can also use :up to save your files. This command only saves a file if a modified buffer is detected.
How to save a file and exit in Vim?
Like the save file command, the command to save a file and quit Vim is :wq. All you have to do is press Esc to get back to command mode, type :wq and press enter.
If you’re looking to exit the editor without saving the file, you can use the :q! command in a similar way.