BASICS

Learning Vim

Chapter 2 — Basics

Understanding the Interface

When you open Vim you are met with a deliberately minimal screen. There are no toolbars, no menus, no panels. This is intentional — every pixel is reserved for your file. Let us decode the few things that are on screen.

The tilde lines

Empty lines at the bottom of the editing area are shown as a single ~ (tilde) character. These are not part of your file — they are Vim's way of saying "there is no content here". This distinction matters: a blank line in your file (a line that exists but contains nothing) looks different from a ~ line. A blank line is truly empty; a ~ line does not exist at all.

  1 This is line one of my file
  2 This is line two
  3
  4 ~
  5 ~
  6 ~

In the example above, line 3 is a real blank line (you pressed Enter to create it). Lines 4–6 with tildes are beyond the end of the file.

The status line

The bottom of the Vim window is the status area. It serves multiple purposes depending on context:

  • Mode indicator: When you enter Insert, Visual, or Replace mode, the current mode name appears here (e.g. -- INSERT --).
  • Command input: When you press :, your typed command appears here.
  • File information: After opening a file, Vim shows its name, line count, and character count briefly.
  • Position: The bottom-right corner shows your cursor position as line,column.
  1 Hello, Vim!
  2 ~
  3 ~
"hello.txt" [New File]                           1,1           All

Reading the status line above: the file is called hello.txt, it is new (not yet saved to disk), the cursor is at line 1, column 1, and All means the entire file fits on screen.

Vim Modes — A Complete Picture

In Chapter 1 we introduced three modes. Now let us look at all five you will encounter, understand how they relate to each other, and learn the correct way to move between them.

Mode Indicator Purpose Enter from Normal
NORMAL (no indicator) Navigation, text manipulation, issuing commands. Vim's home base. It is the default — press Esc to return here from anywhere.
INSERT -- INSERT -- Typing text into the file, just like a conventional editor. i, a, o, I, A, O, s, c
VISUAL -- VISUAL -- Selecting blocks of text to copy, delete, or transform. v (character), V (line), Ctrl+v (block)
COMMAND : prompt at bottom Running editor commands — save, quit, search/replace, settings. :
REPLACE -- REPLACE -- Overwriting existing text character by character (like Insert with overwrite). R

Visual mode and Replace mode are covered in depth in later chapters. For now the key insight is this: Normal mode is the hub. Every other mode is reached from Normal mode, and pressing Esc always brings you back to Normal mode.

Mode transition diagram:

  INSERT  ←── i, a, o ───┐
  VISUAL  ←── v, V ──────┤
  COMMAND ←── : ─────────┼──── NORMAL  (always return with Esc)
  REPLACE ←── R ─────────┘

Normal mode in depth

Normal mode is not a "waiting" mode — it is an active editing mode in which every key is a command. This is Vim's core philosophy: you spend most of your time navigating and manipulating text, with only brief excursions into Insert mode to type new content. In a typical editing session you might be in Normal mode 70–80% of the time.

A few examples of what single keys do in Normal mode (we cover all of these in later chapters):

KeyAction in Normal mode
dDelete operator (combine with a motion: dw = delete word)
yYank (copy) operator
pPut (paste) after cursor
uUndo
DDelete from cursor to end of line
GJump to the last line of the file
xDelete the character under the cursor
The most important rule in Vim: If something unexpected is happening — keys are not doing what you expect, text is being deleted when you want to type, or you are lost — press Esc repeatedly until the mode indicator disappears from the status line. You are back in Normal mode and nothing more will change unexpectedly.

Returning to Normal mode: Esc vs Ctrl+C

There are two ways to return to Normal mode from Insert or Visual mode:

  • Esc — the standard way. Always works, always safe.
  • Ctrl+C — an alternative that most users find slightly faster to reach. It works identically to Esc in almost all situations, with one subtle difference: it will not trigger the InsertLeave autocommand event (relevant only if you use advanced plugins or autocommands — ignore this for now).

Many experienced Vim users remap Caps Lock to Esc at the operating system level, since Esc is used so frequently and is awkward to reach. Another popular choice is to map jj or jk (typed quickly) to Esc in the Vim config. We will cover remapping in Chapter 8.

Insert mode — the creative phase

Insert mode is where you write. Once you are in Insert mode, Vim behaves much like any other text editor: the arrow keys move the cursor, Backspace deletes the previous character, Enter creates a new line, and what you type appears on screen.

The important mental discipline is this: when you have finished writing a section of text, press Esc immediately. Do not stay in Insert mode while you read what you have written or think about what to write next. Return to Normal mode. This habit is what separates Vim users who feel fast from those who feel slow.

Insert mode keyboard shortcuts: Even while in Insert mode there are a handful of useful shortcuts that do not require switching to Normal mode first:
  • Ctrl+W — delete the word before the cursor (equivalent to Backspace × word)
  • Ctrl+U — delete from the cursor back to the start of the line
  • Ctrl+T — indent the current line one shiftwidth to the right
  • Ctrl+D — remove one level of indentation from the current line

File Operations

Opening files

You have already seen vim filename from the shell. There are several variations worth knowing:

# Open a single file
vim notes.txt

# Open multiple files (cycles through them with :n and :prev)
vim file1.txt file2.txt file3.txt

# Open a file and jump to a specific line number
vim +42 script.sh

# Open a file and jump to the first occurrence of a pattern
vim +/"search term" document.txt

# Open a file in read-only mode (prevents accidental edits)
vim -R important.conf

Opening a different file from within Vim

You do not need to quit Vim to open another file. The :e command (short for edit) loads a new file into the current buffer:

:e filename.txt

If you have unsaved changes in the current file, Vim will warn you:

E37: No write since last change (add ! to override)

Either save first with :w, or force the switch and discard changes with :e!.

Tab completion is available when typing filenames after :e. Start typing the filename and press Tab to autocomplete. If there are multiple matches, press Tab again to cycle through them, or press Ctrl+D to display all matches in a list below the command line.

# Example: typing :e kno<Tab> might complete to :e knowledge.txt
:e kno<Tab>       →  :e knowledge.txt
:e script<Tab>    →  :e script_   (cycles: script_one.sh, script_two.sh ...)

Saving files

The write command :w has several useful forms:

# Save the current file (overwrite)
:w

# Save to a different filename (original file unchanged — like "Save As")
:w newfilename.txt

# Save a specific range of lines to a new file (lines 5 through 12)
:5,12w excerpt.txt

# Append the current file's contents to an existing file
:w >> log.txt

# Force save a file opened read-only (if you have filesystem permissions)
:w!
Save vs Save As: :w newname.txt writes the current buffer's contents to newname.txt, but Vim continues to think you are editing the original file. To rename the file you are working on (i.e. change which file this buffer is associated with), use :saveas newname.txt instead — this writes the new file and changes the buffer's name.

Checking the current file

At any point in Normal mode, pressing Ctrl+G displays a summary of the current file in the status line:

"knowledge.txt" line 3 of 10 --30%-- col 1

This tells you the filename, your current line number, the total number of lines, what percentage through the file you are, and your column position. It is a fast way to reorient yourself when you have been navigating through a long file.

For more detail, the command :f (or :file) shows the same information.

Quitting Vim

There are more quit variants than you might expect, and knowing all of them prevents frustration:

CommandBehaviour
:qQuit. Only works if there are no unsaved changes.
:q!Force quit. Discards all unsaved changes.
:wqWrite and quit. Saves, then exits.
:xWrite and quit — but only writes if changes were made. Slightly smarter than :wq.
ZZNormal mode shortcut for :x — no colon needed.
ZQNormal mode shortcut for :q! — force quit without saving.
:qaQuit all open buffers/windows.
:qa!Force quit everything — all buffers, discarding any unsaved changes.
Anatomy of :qa!: Breaking this command down makes every Vim command easier to learn:
  • : — enter Command mode
  • q — the quit command
  • a — modifier meaning "all" (apply to all open buffers)
  • ! — modifier meaning "force" (do not ask for confirmation)
Vim commands often follow this pattern of a base command plus optional modifiers. Once you learn the modifiers they apply consistently across many commands.

Practical Walkthrough — Building a Knowledge File

Let us put everything from this chapter together in a practical session. We will create a file, add some content, practise moving between modes, and explore several ways to save and quit. Follow along in your own terminal.

Workflow — Creating knowledge.txt

1 vim knowledge.txt Open (create) the file from your terminal. Vim opens in Normal mode — the status line shows "knowledge.txt" [New File].
2 :set number Enable line numbers so you can follow along easily. Press Enter after typing the command.
3 i Enter Insert mode. The status line shows -- INSERT --. You are now ready to type.
4 (type your content) Type several lines, for example: "The only source of knowledge is experience" — Albert Einstein. Press Enter between lines.
5 Esc Return to Normal mode. The -- INSERT -- indicator disappears. Good habit: return here whenever you pause to read.
6 Ctrl+G Check your position. The status line shows the filename, current line, total lines, and column. Useful for orientation.
7 :w Save the file. The status line confirms: "knowledge.txt" written.
8 :w backup.txt Save a copy to backup.txt. Note that you are still editing knowledge.txt.
9 :e backup.txt Switch to the backup file to verify the copy is correct. Tab completion works here — try typing :e bac<Tab>.
10 :e knowledge.txt Switch back to the original file.
11 ZZ Save and quit in one keystroke (Normal mode shortcut — no colon needed). You are back at the shell prompt.

Experimenting with the quit variants

Reopen the file and practice the different quit commands so they become muscle memory:

vim knowledge.txt

# Make a change (enter Insert mode, type something, press Esc)
# Then try quitting without saving:
:q           # Vim refuses — there are unsaved changes
              # Output: E37: No write since last change (add ! to override)

:q!          # Force quit — change is discarded

# Reopen, make another change, then save and quit:
vim knowledge.txt
# ... make a change ...
ZZ            # save and quit — fastest method

Commands Introduced in This Chapter

Chapter 2 — Command Reference

Ctrl+CAlternative to Esc — return to Normal mode
Ctrl+GDisplay filename, line number, and position in the status line NORMAL
ZZWrite and quit (only writes if changes were made) NORMAL
ZQForce quit without saving NORMAL
Ctrl+W (Insert)Delete the word before the cursor INSERT
Ctrl+U (Insert)Delete from cursor to start of line INSERT
:e <file>Open (edit) a file; Tab key completes the filename
:e!Re-load the current file from disk, discarding unsaved changes
:w <file>Write buffer contents to a new filename (Save As)
:saveas <file>Write to a new filename and switch the buffer to that file
:xWrite (only if changed) and quit — smarter version of :wq
:qaQuit all open buffers
:qa!Force quit all buffers, discarding all unsaved changes
vim +N <file>Open file and jump to line N from the shell
vim -R <file>Open file in read-only mode from the shell