Home » Linux » Applications » Text Editors » Save File And Quit In Vim

How to Save a File and Quit in Vim/Vi

In this guide, we explain how to save a file and quit/exit in Vim, how to save a file without exiting, and how to quit/exit without saving. Vim (Vi IMproved) is an open-source text editor for Unix or Linux. It’s used to write and edit text, either in the command line interface or independently through a GUI. It’s a great tool and like any software tool, one of the first things to learn about it is how to safely exit without losing all of your work.

Vim Modes

Before you save and exit, you’ll obviously need to have something to save. The default mode, when Vim opens, is command mode. In this mode, you can move the text around, edit and delete it, but you can’t insert any new text. In this mode, all alphanumeric keys are commands. For example, pressing “j” moves the cursor down one line. That’s not ideal. To add text, you need to be in Vim’s insert mode. Get there by pressing the “i” key and get back to command mode by pressing ESC.

Saving and Exiting in Vim

As you’d expect, getting out of Vim is one of the simplest things to do.

Save and Exit

This is a really simple 3-step process that saves the file you’re actively working on and exits Vim. It does not automatically save every open file.

  1. First, make sure you’re in command mode by pressing ESC.
  2. Open the prompt bar in the bottom left corner of the window by pressing the colon key (:)
  3. Type “x” after the colon, then press Enter.

As soon as you press enter, your changes will be saved and Vim will close.

There’s also a single-step version of this set of commands. You can achieve the same result just by typing “:wq”

Save without Exiting

Obviously, you usually don’t want to close the entire tool when you save your work. At least, not if you’re following good practice and saving your work regularly. This method is also a three-step process.

  1. First, make sure you’re in command mode by pressing ESC.
  2. Open the prompt bar in the bottom left corner of the window by pressing the colon key (:)
  3. Type “w” after the colon, then press Enter.

As soon as you press enter, your changes will be saved.

Another common operation is to save a copy of your work. That’s a very similar process. Instead of just typing “w”, type “w” [new file name]. For example:

:w fileversion2

The active file is saved with the new filename and your previous work is stored safely in a file with the old name so that you can refer to it as a backup if you need to. Note that Vim always saves files in .txt format.

Exit without Saving

Suppose you just opened a file to refresh your memory about a line of code, or maybe you messed something up so badly you just want to close the tool and start again. This, too, is a really simple operation.

  1. First, make sure you’re in command mode by pressing ESC.
  2. Open the prompt bar in the bottom left corner of the window by pressing the colon key (:)
  3. Type “!q” after the colon, then press Enter.

As soon as you press enter, the program will close.

It takes a brave person to just quit out of a program without saving. Even if you’re sure you don’t want to keep the code you’ve just written or edited, it’s often worth saving a new copy as a backup, using the method described above. If it turns out you were right and you don’t need it, you can always delete it, but there’s no way of getting something back after a quit without saving.

Saving and exiting are really simple operations in Vim, as you would hope. They’re a good way to start getting used to using keyboard shortcuts, which will stand you in good stead as you learn to do more with Vim.

SHARE:
Photo of author
Author
My name is Stefan, I'm the admin of LinuxScrew. I am a full-time Linux/Unix sysadmin, a hobby Python programmer, and a part-time blogger. I post useful guides, tips, and tutorials on common Linux and Programming issues. Feel free to reach out in the comment section.

Leave a Comment