Introduction

Vim (Vi IMproved) is a well-known, open-source text editor for Unix systems and macOS. It is a powerful and stable tool for editing and writing text, both in the command-line interface and as an independent application in a GUI.

Take the first step by learning some of the basic saving commands and building up from there.

Follow this guide and learn how to open, save, and exit a file in Vi/Vim.

how to save a file in vim

Prerequisites

  • Ubuntu 18.04 Bionic Beaver
  • A user with sudo privileges
  • Access to a terminal/command line

How to Save a File Without Exiting in Vi / Vim

If you want to save a file without exiting the text editor:

1. Switch to command mode by pressing the ESC key.

2. Press : (colon). This will open the prompt bar in the bottom left corner of the window.

3. Type w after the colon and hit Enter. This will save the changes made to the file, without exiting.

You can also rename an existing file by adding the new name after the commands.

:w [newfilename]

In the picture below, we renamed the existing example1.txt into newexample.

How to Save a File and Quit in Vi / Vim Text Editor

If you want to save a file and exit the text editor:

1. Switch to command mode by pressing the ESC key.

2. Press : (colon) to open the prompt bar in the bottom left corner of the window.

3. Type x after the colon and hit Enter. This will save the changes and exit Vim.

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

:wq

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

How to Exit a File Without Saving in Vi / Vim

To exit a file without saving:

1. Switch to command mode by pressing the ESC key.

2. Press : (colon) to open the prompt bar in the bottom left corner of the window.

3. Type q! after the colon and hit Enter. This will exit Vim without saving the changes.

How to Open a File in Vi or Vim

To open a file in the Vim editor, run the command:

vim [filename]

or

vi [filename]

This will either open an existing file with that specific name or create a new one and open the text editor.

In this tutorial, we created a file with the name example1.txt

Command mode vs insert mode

Once the editor has launched, it will open a file in command mode. This means that you can move around and edit the file, but you cannot insert new text into it. All alphanumeric keys in this mode are equal to commands and pressing them will not display their value on the screen.

For example, pressing the letter w will move the cursor one word forward.

To insert text, you must be in insert mode. To switch to insert mode, press the i key. Now you can insert text into a file.

To switch back to command mode, press the ESC button.

Conclusion

This article explains how to open, save and exit a file in Vi/Vim. Once you have mastered these elementary commands, you can focus on studying the basic navigation commands used in command mode.

The more comfortable you feel using keyboard shortcuts, the more you will understand why Vim earned its worldwide reputation.