How to Save a File in Vi / Vim & Exit

Introduction

Vim (Vi IMproved) is a versatile and open-source text editor designed for Unix and Linux systems. Renowned for its power and stability, Vim excels in text editing and writing, whether you’re using the command-line interface or a graphical user interface (GUI).

Begin your journey with Vim by mastering the essential save and exit commands. This guide will teach you how to save a file in Vi/Vim and exit the editor.

Prerequisites

  • A system running Linux.
  • A user with sudo privileges.
  • Access to a terminal/command line.
  • Vim installed (or use the default Vi).

Using Vim Modes

When Vim launches, files open in command mode by default. In this mode, you can navigate and edit the file, but you cannot insert new text.

All alphanumeric keys act as commands in command mode, and pressing them will not display their characters on the screen. For example, pressing the letter w will move the cursor one word forward.

To type text, you need to switch to insert mode. Press the i key to enter insert mode, allowing you to type text into the file.

To return to command mode, press the Esc button.

How to Save Changes in Vim

Vim provides various commands for saving changes to a file, depending on whether you also want to exit the text editor. Here are examples showing how to save and exit, save without exiting, and exit without saving.

Begin by opening a new text file in Vim:

vim example_file.txt

Next, press i to enter insert mode and add some text.

Vim Save and Quit Command

To save a file and exit Vim, do the following:

  1. Switch to command mode by pressing the Esc key.
  2. Press : (colon) to open the command prompt at the bottom left corner of the window.
  3. Type x after the colon and press Enter. This will save your changes and exit Vim.
Vim Save And Quit The Editor Command

Alternatively, a command that will also save a file and exit the text editor is:

:wq

In which case the stands for write (save) and q stands for quit.

How to Save a File in Vi / Vim Without Exiting

To save a file without exiting in Vim:

  1. Switch to command mode by pressing the Esc key.
  2. Press : (colon) to open the command prompt at the bottom left corner of the window.
example of saving a file without exiting in Vim
  1. Type w after the colon and press Enter. This will save the changes made to the file in Vim without exiting.

To rename the file, add the new name after the command.

:w [newfilename]

In the image below, we renamed the existing file example1.txt to newexample.

Exit Without Saving Changes in Vi / Vim

To exit Vim without saving changes:

  1. Switch to command mode by pressing the Esc key.
  2. Press : (colon) to open the command prompt at the bottom left corner of the window.
  3. Type q! after the colon and press Enter to exit without saving the changes.
command to exit vim without saving changes

The q! quits the text editor.

Conclusion

This article explained how to save files in Vi/Vim using various command options.

As you become more comfortable with keyboard shortcuts, you’ll better understand why Vim has earned its worldwide reputation.

Leave a Comment