How to Save Files in Vim Before Quitting the Vim Editor

Suraj Joshi Feb 02, 2024
  1. Open the Files With Vim/Vi Editor
  2. Save the File in Vim Editor
  3. Save the File in Vim and Quit From the Editor
  4. Quit From Vim Without Saving the Changes
How to Save Files in Vim Before Quitting the Vim Editor

Vim and its earlier version Vi are among the most widely used and popular editors among users who use Command-Line Interface frequently. The different modes of operation in Vim and Vi make them quite different, and hence beginners might find them difficult to use.

There are two modes of operation in Vim and Vi. The two modes in Vim are Normal and Insert mode. Normal mode is the default mode of Vim, and Insert mode is similar to other text editors in which anything we type appears on the screen.

We use the commands :w to save the files in Vim with quitting, :q to exit from the editor, and :wq in normal mode to save the files and quit the editor.

Open the Files With Vim/Vi Editor

To open any files using Vim/Vi, we use the vim command followed by the file’s path to be opened using the editor.

vim abc.txt

It will open the file abc.txt in the Vim editor.

We can also open the editor using vim command and then open the file using the below command:

:e filename

filename is the name of the file to be opened.

Save the File in Vim Editor

To save the file in Vim without exiting the editor, we perform the following steps:

  • Switch to Normal mode

    If we are in Insert mode, we can use Esc key to switch back to Normal mode.

  • Type :w command
  • Press Enter key

    It will save the changes in the file opened in Vim.

To save the file with a different name, we use the following command:

:w filename_updated

It will save the filename with the name filename_updated.

Save the File in Vim and Quit From the Editor

To save the file in Vim and quit from the editor, we perform the following steps:

  • Switch to Normal mode
  • Type :wq command
  • Press Enter key

    It will save the changes in the file opened in Vim and exit from the editor.

We can also use the command :x to save the file in Vim and quit the editor. The command :x only saves the file changes before exiting while the command :wq also updates the file modification time.

Quit From Vim Without Saving the Changes

To quit from Vim without saving the changes, we perform the following steps:

  • Switch to Normal mode
  • Type :q! command
  • Press Enter key

    It will exit from the editor without saving the changes.

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

Related Article - Linux Vim