The Basics of Saving and Quitting in Vim

Matrix phone booth as agent terminal closes in, and a cute, helpful terminal comes to man in phonebooth with ":q" Vim command.

Now you’ve done it. You’re in terminal land and found yourself stuck inside a Vi or Vim editor and don’t know how to quit, let alone save your changes. CTRL+C doesn’t work its magic like usual. So how did others manage to escape?

Fun fact: the first time I found myself in Vim in class, I restarted my computer so I could quit Vim. This was in the before-times when smartphones weren’t mainstream.


Quit Without Saving

You don’t care about your changes, or you don’t want to make changes. You just want to quit.
In order to quit, you just need to press the following keys in order.

  1. ESC
    • Escape key – guarantees you’re back in NORMAL mode where the cursor is a block
  2. :
    • Colon key – puts Vim into “command” mode
  3. q
    • The letter Q, lowercase – the command to quit
  4. !
    • Exclamation mark – when quitting, discard any changes
  5. ENTER

On the Vim status line, you should see the following before pressing enter.

Vim Status Line
:q!

Save and Quit – Existing File

You’ve made some changes and want to save them, then quit. Here are the key strokes to do just that.

  1. ESC
    • Escape key – guarantees you’re back in NORMAL mode where the cursor is a block
  2. :
    • Colon key – puts Vim into “command” mode
  3. w
    • The letter W, lowercase – the command to “write” or save the file
  4. q
    • The letter Q, lowercase – the command to quit
  5. ENTER

On the Vim status line, you should see the following before pressing enter.

Vim Status Line
:wq

Save and Quit – New File

You opened Vim without a file, discovered Vim’s insert mode, wrote a masterpiece, and now you want to save it before you quit.
Follow the keystrokes to save this file and quit Vim.

  1. ESC
    • Escape key – guarantees you’re back in NORMAL mode where the cursor is a block
  2. :
    • Colon key – puts Vim into “command” mode
  3. w
    • The letter W, lowercase – the command to “write” or save the file
  4. q
    • The letter Q, lowercase – the command to quit
  5. SPACE
    • Leave a space between the command list and the next argument
  6. <FILEPATH>
    • You need to enter a filename to save it in the current directory (where you opened Vim in the Terminal), or a full file path to save the file elsewhere. See the examples below.
  7. ENTER

Save File to Current Directory

Using the current directory, let’s save the file as i-luv-craft-automata.txt.
On the Vim status line, you should see the following before pressing enter.

Vim Status Line
:wq i-luv-craft-automata.txt

Save File to Home Directory

Let’s save the file to our home directory with the filename craft-automata-luvs-vim.txt.
On the Vim status line, you should see the following before pressing enter.

Vim Status Line
:wq ~/craft-automata-luvs-vim.txt

Save File to Any Path

You can save a file to any path, as long as you have the target’s directory’s relative or absolute path.
Let’s say we want to save this to the directory above, and we want to use the filename mac-os-comes-with-vim-installed.txt.

On the Vim status line, you should see the following before pressing enter.

Vim Status Line
:wq ../mac-os-comes-with-vim-installed.txt



Leave a Reply

Your email address will not be published. Required fields are marked *