Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 36 ยท Getting Started

Your First Edit: Open, Type, Save, Quit

Open a file in Vim, enter Insert mode, type, save with :w, quit with :q, and recover when you get stuck. Includes a buffer simulator to practice safely.

The first thing everybody asks about Vim is "how do I quit?". This lesson answers it and then goes further: you will open a file, add text, delete a line, save, and exit, and you will learn to read what Vim tells you along the way.

Open a file

From a terminal:

Terminal
vim shopping.txt

If the file does not exist, Vim opens an empty buffer with that name and the message "shopping.txt" [New]. Nothing is created on disk until you save. The screen shows the file text, tildes ~ for lines past the end, and a status area at the bottom.

A buffer is the in-memory copy of the file. You edit the buffer; writing saves it to disk. Vim keeps track of whether the buffer has been modified, and refuses to quit if it has unsaved changes. That protection explains most "I cannot quit" panics.

Type some text

You start in Normal mode, so typing letters will not insert them. Press i (insert before the cursor). The bottom of the screen shows -- INSERT --. Type:

TEXT
milk
eggs
bread

Press Enter between lines as usual. When done, press Esc. The cursor steps one character left and the mode indicator disappears: you are back in Normal mode.

Other ways to enter Insert mode, which you will use constantly:

Key Starts typing
i before the cursor
a after the cursor
I at the first non-blank of the line
A at the end of the line
o on a new line below
O on a new line above

Save and quit

All of these commands begin with :, which opens the command line at the bottom. You finish with Enter.

Command Effect
:w write (save) the buffer
:q quit; fails if there are unsaved changes
:wq write and quit
:x or ZZ write only if changed, then quit
:q! or ZQ quit and throw away changes
:w newname.txt save a copy under another name

ZZ and ZQ are Normal-mode keys and need no colon or Enter.

:q! discards every unsaved change without asking. Use it deliberately. If Vim says E37: No write since last change (add ! to override), it is protecting you; save with :w first unless you truly want to lose the edits.

Delete and undo

In Normal mode, x deletes the character under the cursor, dd deletes the whole line, and u undoes. Ctrl-r redoes. Try deleting eggs, then undoing. You will see a message such as 1 line less or 1 more line, which tells you what happened.

A buffer simulator (stand-in)

Vim is not available here, so this block simulates the save and quit rules. It is a teaching model of the modified flag, not real Vim. Pass a command list and watch the messages.

JS Save-and-quit rules (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Change the commands to see each message: try q! right after a delete and compare s.disk with s.lines.

When you are stuck

Three situations cover almost every panic.

  1. Typing does weird things. You are in Normal mode. Press i if you meant to type, otherwise Esc then u.
  2. The bottom shows : or / and nothing happens. You are in the command line. Press Esc to cancel.
  3. The screen froze after Ctrl-s. That is the terminal's flow-control stop, not Vim. Press Ctrl-q to resume.

If Vim crashes, or the connection drops, you may see a swap-file warning next time (E325: ATTENTION). The swap file .shopping.txt.swp holds your unsaved changes. Choose (R)ecover to restore, then save and delete the swap file. Choose (D)elete only if you know the changes are not needed.

Reading the status line

At the bottom right Vim shows the position, for example 2,4 All, meaning line 2 column 4 and that the whole file fits on screen. Press Ctrl-g to see the file name, line count and modified flag; [+] after the name means unsaved changes. This is the quickest way to check before quitting.

A second stand-in helps you read these markers. Edit the file state and see what the status string would say.

JS Status line reader (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. You opened a file, typed text, and Vim answers E37 when you type :q. What should you do?

    E37 means unsaved changes. Save with :w (or :wq), or discard on purpose with :q!.
  2. 2. Which key opens a new line below the current line and starts Insert mode?

    o opens a line below; capital O opens one above.
  3. 3. What does ZZ do in Normal mode?

    ZZ is the same as :x, and ZQ is the same as :q!.
  4. 4. What does [+] after the file name mean?

    [+] marks a modified buffer. It disappears after :w.

Summary

  • A buffer is the in-memory copy; :w writes it, :q quits, :wq does both.
  • i a I A o O enter Insert mode in different places; Esc returns to Normal.
  • Vim refuses to quit with unsaved changes (E37) unless you use !.
  • x, dd, u, Ctrl-r delete, delete a line, undo and redo.
  • A swap file offers recovery after a crash.

Next: The Vim Grammar: Verbs, Counts and Motions