How To Save A File In Vim/Vi?

Vim is a useful command-line text editor. Vim can save files in different ways by using the commands or key shortcuts.

Save File with write Command

The write command is used to save or write changes into the current file. In order to use the write command first, the normal mode should be enabled.

  • Press ESC key to change normal mode.
  • Type :write in order to save file and then press Enter.
Save File with write Command

The Vim uses the shortcuts regularly and the :w can be used as a shortcut for the :write command.

  • Press ESC key to change normal mode.
  • Type :w in order to save file and then press Enter.

When the write or save operation is completed some information will be provided about save under the Vim.

  • sample.txt is the file name which is saved.
  • 17L is the line numbers which are saved.
  • 3122C is the characters count which are saved.

Save File with Different Name

By default, the save operation will save the changes into the current file with the same file name. But you can save the changes into a new file and continue with this file by providing the file name to the :write or :w commands. In the following example, we will save the file as a new file with the name mysample.txt.

  • Press ESC key to change normal mode.
  • Type :write mysample.txt in order to save file and then press Enter.

Or as a shortform.

  • Press ESC key to change normal mode.
  • Type :w mysample.txt in order to save file and then press Enter.
Save File with Different Name

Save File and Quit

Vim provides different commands in order to save file and exit. This is very useful for fast operation. We will provide the wq commands which is shortform of the write and quit.

  • Press ESC key to change normal mode.
  • Type :wq in order to save file and then press Enter.
READ  List Upgradeable Packages with apt In Ubuntu

Alternative way is using the x command according to the wq command.

  • Press ESC key to change normal mode.
  • Type 😡 in order to save file and then press Enter.

Quit Without Saving File

You can make different changes in a file and then want to not save these changes and exit from file without saving changes. You can use the quit! or q! commands which will quit or exit from the Vim without saving changes.

  • Press ESC key to change normal mode.
  • Type :quit! in order to save file and then press Enter.

Or

  • Press ESC key to change normal mode.
  • Type :q! in order to save file and then press Enter.

Leave a Comment