Vim save and quit

Vim or VI Improved is a powerful open-source text editor used primarily for writing and editing text.

Essentially, Vim is a text editor found in Unix and Linux based systems, and it’s also one of the oldest and stable text editors there are.

One of the basic steps to learn is how to use Vim’s basic commands. In this guide, we’ll show you how to perform the save and exit function using Vi/Vim.

First things first…

Requirements

You’ll need to set up a couple of things before saving in Vim or if you want to exit from Vim. More importantly, you need to have Vim installed along with a few privileges to use it.

  • The user should have sudo privileges
  • The user should have access to the command line
  • Ubuntu 18.04 Bionic Beaver (OS version)
  • Apt tool (package tool pre-loaded in Debian and Ubuntu distros)

Now, what if you want to use Vi in Windows? First, you’ll have to download the installer through this link: https://www.vim.org/download.php

Once you’ve downloaded the file and proceed to the installation setup, the installer will ask for the installation type. Hit “Full” and then hit Enter.

Once it’s installed successfully, open the “Run Application in Windows and type “cmd” in the command line. On the screen prompt, type “vim” and press Enter.

Launching the Vi/Vim Editor

Once you launch Vi/Vim, you will be in command mode. When command mode is active, you can insert any new text, only editing functions apply.

All alphanumerical keys in command mode display different values than when you’re in insert mode. For example, hitting “w” in command mode will move one word forward, and “b” will move one word backward.

To enter insert mode, simply type “i” and hit Enter. From there, you can now type text into the editor.

If you want to switch back into command mode, simply press the “Esc” key.

Opening a File

If you want to open a file in Vim, simply type “vim” followed by the file name. Make sure you’re in command default mode as well. If not, simply press Esc to switch back to the default mode.

To open a file, simply type:

“vim” file.txt

Save & Quit commands in Vim

There are three ways to use the save & quit commands in Vim:

  1. Save a file and exit Vim
  2. Save a while without exiting Vim
  3. Exit without saving

Save a File and Exit Vim

  1. If you’re currently in insert mode, switch back to command mode by pressing the Esc key.
  2. Hit colon (:). This will prompt open a bar in the bottom-left part of your screen
  3. Type “x” after the (:) colon and then hit Enter. This will allow you to save a file in Vim and exit. Alternatively, you can type “wq” after the (:) colon to also save a file in Vim and exit.

Save a File Without Exiting Vim

  1. Switch back to command default mode by pressing the Esc key.
  2. Hit colon (:). This will prompt open a bar in the bottom-left part of your screen.
  3. Type “w” after the (:) colon and then hit Enter. This will allow you to save a file in Vim and not quit Vim.

You can also use “w” to make changes to a filename, for example:

:w [newfilename]

Exit Without Saving Any Changes Made

This command works if you don’t want to quit Vim and not save a file or save any changes made.

  1. Switch back to command default mode by pressing the Esc key.
  2. Hit colon (:). This will prompt open a bar in the bottom-left part of your screen.
  3. Type “q!” after the (:) colon and then hit Enter. This will allow you to perform a Vim exit without saving any changes made.

Other Useful Commands

Now that you know how to save a file in Vim, exit, and open a file using the Vim editor, in the event you want to open more than one file on Vim, you can do that as well.

The basic commands “type q/w/x” are useful when handling a single file on Vim editor. With more than one file open, use these as a guide:

Note: Remember to press Esc before you type these in. “Press Esc” is the default text to type so you can switch from insert to command default mode.

  • “:xa” This will save all the changes/multiple files and exit Vim.
  • “:wa” This will save all the changes.
  • “:qa” This will close Vim but will only exit once a file or any changes are saved.
  • “qa!” This will exit Vim without saving a file or any changes made.

Before You Go…

Now that you understand the basics of how to use Vim editor such as saving a file, quitting, and switching from insert to command default mode, this is only the beginning!

Compared to other text editors, Vim might have a higher learning curve, but the payoff is also worth the process.

Being proficient with Vim allows you to edit your texts and files easily just by using your keyboard, and the work you can accomplish will be much faster.

The more you acquaint yourself with Vim’s various uses, you’ll also see why a vast number of users benefit highly from it. What are you waiting for? Start using those commands to its full potential, and start exploring Vim!