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):
| Key | Action in Normal mode |
|---|---|
d | Delete operator (combine with a motion: dw = delete word) |
y | Yank (copy) operator |
p | Put (paste) after cursor |
u | Undo |
D | Delete from cursor to end of line |
G | Jump to the last line of the file |
x | Delete the character under the cursor |
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 toEscin almost all situations, with one subtle difference: it will not trigger theInsertLeaveautocommand 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.
Ctrl+W— delete the word before the cursor (equivalent toBackspace× word)Ctrl+U— delete from the cursor back to the start of the lineCtrl+T— indent the current line one shiftwidth to the rightCtrl+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!
: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:
| Command | Behaviour |
|---|---|
:q | Quit. Only works if there are no unsaved changes. |
:q! | Force quit. Discards all unsaved changes. |
:wq | Write and quit. Saves, then exits. |
:x | Write and quit — but only writes if changes were made. Slightly smarter than :wq. |
ZZ | Normal mode shortcut for :x — no colon needed. |
ZQ | Normal mode shortcut for :q! — force quit without saving. |
:qa | Quit all open buffers/windows. |
:qa! | Force quit everything — all buffers, discarding any unsaved changes. |
:qa!: Breaking this command down makes every Vim command easier to learn:
:— enter Command modeq— the quit commanda— modifier meaning "all" (apply to all open buffers)!— modifier meaning "force" (do not ask for confirmation)
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
"knowledge.txt" [New File].
-- INSERT --. You are now ready to type.
-- INSERT -- indicator disappears. Good habit: return here whenever you pause to read.
"knowledge.txt" written.
backup.txt. Note that you are still editing knowledge.txt.
:e bac<Tab>.
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
Esc — return to Normal mode:wq