How to Save and Quit Files in Vim


Vim is a powerful text editor that comes with Linux, BSD, and macOS. Using Vim as a beginner is little difficult due to its unique command-driven interface and non-standard keyboard shortcuts compared to other text editors. In this guide, we will be going to show how to save and quit a file in Vim. Let’s get started.

When you open a file in Vim, it opens in Normal mode. To edit the file, you need to switch to Insert mode by pressing the i key. To return to Normal mode, you can press the Esc key.

Save Files in Vim

To open a file in Vim, you need to simply type:

vim filename.txt

This will open the file in Vim editor. Go to Insert mode to edit the file. Once you are done editing to a file, press the Esc key on your keyboard to switch to Normal mode. Then to save the changes, type :w and press Enter.

Save and Quit Files in Vim

Once you are done writing to the file, press the Esc key on your keyboard to switch to Normal mode. Then to save the file, enter:

:w

When you use the above commands, vim will give you a warning if you are modifying the read-only file.

 

 

 

 

 

To force the action, use :w! to overwrite the existing file.

Then to quit the editor, enter:

:q

You can also save and quit the file altogether using:

:wq

Another way to save and quit the file is to type the following and press Enter in the Normal mode.

:x

Quit Without Saving

If you have modified a file but you no longer want to save it, you can force quit it. To quit a file in Vim without saving it, go to Normal mode by pressing the Esc key. Then type the following and press Enter.

:q!

This will quit the file without saving any changes.

Vim is a powerful text editor with plenty of features and keyboard shortcuts. Understanding how to save and quit a file is the basic skill every user should know when starting their journey with Vim. Once you learned how to save and quit the file in Vim, you are ready to explore Vim and its features.

Print Friendly, PDF & Email