How to Save a File in Vim or Vi and Quit the Editor on Linux

You need to enter the command mode in vim / vi to save and exit a file.

vim is one of the most popular file editors in Linux. Part of the popularity is due to the command line mode of vim, which enables users, especially software developers and advanced users to optimize time for file modification operations.


In this article, we will see how to save and exit a file in vim command line mode.

An open file in vim looks like the following:

While the file is being edited, it is the ‘Insert’ mode of vim which is active, as signified by the -- INSERT -- text on the bottom line on the terminal. To save the file we need to go to ‘Command’ mode of vim.


To enter command mode in vim, press the Escape key on your keyboard. In Command mode, user can directly start typing vim commands; the bottom-most part of the screen acts as an integrated command prompt.

To save a file in vim, type :w command and press Enter.

As we see, vim displays the number of lines and characters ‘written’, i.e., saved to disk. Similarly, :q is the command used to exit the file, without saving the changes.

To save and exit a file in vim, use command :wq and hit the Enter key.