Editing with Vim
On this page
You open a text editor and press a letter, but the letter does not appear in the file. The editor interprets that key as an editing command.
By the end, you can tell three modes apart, add and change text, search, save, and return to Bash. You also have a deliberate way to leave without saving an unwanted edit.
Before you start
Section titled “Before you start”Finish Editing with Nano (B07.3). Keep the existing B04 practice directory and earlier B07 exercises.
The Ubuntu Desktop installation used in this course does not include the vim command. Its vi command runs Vim-tiny 9.1, a small build of Vim. All commands below therefore use vi. Installing full Vim belongs to B12; it is not a prerequisite for this exercise.
On another installation, check vi --version. If vi is absent or is not Vim, defer this exercise until the installation lesson; do not assume that every program named vi has the same behavior. On the course VM, /usr/bin/vi leads to /usr/bin/vim.tiny.
Use only ~/lab/b07/vim. Run this setup once; use Reset this lesson before repeating it.
mkdir -p ~/lab/b07/vimcd ~/lab/b07/vimprintf '# Text practice\ntopic=files\nstatus=draft\n' > demo.confThe file is a practice configuration, not settings loaded by your system. The printf setup writes three lines to this disposable file, as in B04.
Open in Normal mode
Section titled “Open in Normal mode”vi demo.conf
- The three original configuration lines are visible; color does not identify the active mode.
- The file message names demo.conf. The default screen has no Insert indicator.
Vim starts in Normal mode: keys move around or perform edits rather than inserting their letters. The VM’s default /etc/vim/vimrc.tiny selects Vi-compatible behavior. The screenshots retain it; there are no capture-only launch options.
The colored first line is an observed appearance of this terminal session, not a mode indicator. This build initially shows no Insert banner. Learn the mode transitions rather than using text color as evidence of a mode.
In words: i or o changes Normal to Insert, and Esc returns. From Normal, : begins an editor command and / begins a search on the command line. Enter executes the examples shown; Esc cancels them. This model omits other Vim modes and commands such as :q that leave the editor.
Make the typing mode visible
Section titled “Make the typing mode visible”Press Esc, then type the following inside vi, including its colon:
:set showmode
- The visible editor command is :set showmode. Press Enter to apply it to this session.
The colon opens Command-line mode, where the bottom row receives an editor command rather than file text. Press Enter. set showmode enables the mode display for this editor session; it does not write a configuration file.
Press lowercase i, but do not type anything else yet:

- After i, the genuine – INSERT – banner shows the typing mode.
The genuine -- INSERT -- banner appears. Insert mode puts ordinary typed characters into the buffer, like typing in Nano. Press Esc to return to Normal without adding text. The banner disappears; this build does not replace it with a Normal banner.
Move, then add one line
Section titled “Move, then add one line”These keys are interpreted in Normal mode:
| Keys | Action |
|---|---|
h, j, k, l |
Move left, down, up, right |
1G |
Go to the first line: digit 1, then capital G |
i |
Enter Insert before the cursor |
o |
Open a line below and enter Insert |
cw |
Change the word at the cursor, entering Insert |
| Esc | Return from Insert to Normal |
Use h and l to move across the first line, and j and k to move down and up. Do not expect these letters to be inserted while you are in Normal mode.
Predict first: after o, will typing review=ready run movement commands or add a line to the file?
Run it, then compare with your guess
Press Esc, type 1G, then press j twice. You reach status=draft. Press lowercase o and type review=ready without Enter.

- The added review=ready line is in the buffer.
- The editor is still in Insert mode; Esc returns to Normal.
o created a line below the current one and entered Insert. The text belongs to that line; it is not a sequence of movement commands. The banner shows why the keys have a different meaning.
Press Esc before continuing. The edit is still only in the buffer until you write it.
Search from Normal mode
Section titled “Search from Normal mode”Type /review without Enter:

- The /review search is on the command line; review is highlighted in the buffer while the search is typed.
The slash opens a forward search. In the screenshot above, review is highlighted in the buffer while /review is being typed. Press Enter to run the search:

- After Enter, the cursor lands on the r of review; the temporary search highlight is gone.
- The search wrapped: search hit BOTTOM, continuing at TOP.
The captured cursor lands at the first letter of review. The bottom message says the search reached the bottom and continued at the top: it wrapped to find the same line. After Enter, this screenshot shows no highlight on the matching word, only the block cursor on its first letter. The typed-search highlight in the previous screenshot and the cursor after submission are different states.
n repeats the last search forward; capital N searches in the reverse direction. This fixture has one review line, so another forward search can wrap back to it.
Save, leave, and read the file
Section titled “Save, leave, and read the file”From Normal mode, type :w:

- The :w editor command writes the buffer when you press Enter.
Press Enter to write the buffer to demo.conf. Then type :q, Enter, to quit. Those are editor commands, not Bash commands. You can also use :wq, Enter, to write and quit together.
Read the saved file after the shell prompt returns:
cat demo.conf# Text practicetopic=filesstatus=draftreview=ready
- cat reads the four saved lines after vi exits.
- The prompt belongs to Bash again.
These four lines were captured on the course VM. The original lines remain, and the added line is saved. The filename suffix .conf is a naming convention here; no system service reads this disposable file.
Read the refusal before forcing an exit
Section titled “Read the refusal before forcing an exit”Reopen with the same plain command:
vi demo.confPress Esc, type 1G, press i, type TEMPORARY , then Esc. Try :q, Enter, without saving that disposable test change:

- E37 refuses to quit because the current buffer has unsaved changes.
The captured message is E37: No write since last change (add ! to override). It means the buffer has unsaved changes. The refusal protects you from losing them accidentally; it is not a permission error.
To keep those changes, use :w before quitting. For this temporary edit only, use :q!, Enter, to discard it. If a message asks you to press Enter first, do so before entering the command. The exclamation mark deliberately overrides the unsaved-change check.
cat demo.conf# Text practicetopic=filesstatus=draftreview=readyThe previously saved file remains. Like Nano’s discard choice, :q! does not undo a write already made to disk.
Mistakes to recognize
Section titled “Mistakes to recognize”Typing a command while still in Insert. A colon would become file text there. Press Esc first, then enter :w or :q at the bottom command line.
Pressing q as in less. Normal-mode keys in an editor are not pager controls. Use Esc, then :q, Enter. If unsaved changes exist, read E37 and choose whether to save or discard them.
Assuming search always looks the same. In this captured session, review is highlighted while /review is typed. After Enter, that highlight is gone and the cursor rests on its first letter. Other builds and user settings can present different search states.
Reopening a file with a swap file. You may see E325, an ATTENTION warning saying it found a swap file named .demo.conf.swp. A swap file holds editor recovery data. It can belong to another open vi session, or remain after a terminal closes while vi is running.
The warning offers Open Read-Only, Edit anyway, Recover, Delete it, Quit, and Abort. For this course, press Q to quit that warning rather than choosing Edit anyway. Then check the other terminal windows. If another vi session has demo.conf open, return to it, save work you need, and exit that editor before reopening the file. Do not remove its swap file while it is still editing.
If no other session is editing the file and only a leftover swap file remains, return to this lesson’s directory and inspect the exact name:
cd ~/lab/b07/vimls -l .demo.conf.swprm .demo.conf.swpvi demo.confTry it yourself
Section titled “Try it yourself”Goal: Change a value in a separate handoff while preserving the saved demonstration file.
cp demo.conf handoff.confvi handoff.confFind draft, change that word to checked, save, and exit. Keep the other three lines intact.
Hint
Search positions the cursor on the word. cw changes that word and enters Insert; Esc ends the inserted replacement.
Solution
- Esc,
1G,/draft, Enter. cw, typechecked, Esc.:w, Enter, then:q, Enter.
cat handoff.conf# Text practicetopic=filesstatus=checkedreview=readyThis saved handoff was captured on the course VM. The source demo.conf still has status=draft.
Reset this lesson
Section titled “Reset this lesson”Close this lesson’s editor first, saving or discarding deliberately. Inspect the directory from its parent:
cd ~/lab/b07ls -AR vimAfter the challenge it contains demo.conf and handoff.conf. Do not remove an editor’s working directory while its session is still open.
rm -r ~/lab/b07/vimRerun Before you start to restore the three-line fixture. If the directory is absent, skip removal. Reopened sessions use the VM’s default configuration again; :set showmode was not saved as a permanent setting.
- On the course VM, plain
viruns Vim-tiny; full Vim installation belongs to B12. - Normal interprets editing keys, Insert adds text, and Command-line receives a command or search.
- Esc returns from Insert;
:set showmodemakes Insert visible in this session. :wwrites,:qquits, and:q!deliberately discards unsaved edits.- Read the saved file from Bash to verify the result.
Next, Text format surprises examines differences that an ordinary text display can hide.
Further reading
Section titled “Further reading”- On your system:
man vi; the VM’svi --versionand/etc/vim/vimrc.tinyidentify this build and its defaults. - Vim user manual: The first steps in Vim and Moving around. These describe full Vim; this lesson uses only the captured subset available in Vim-tiny.
- Vim reference: Options, including
showmodeandcompatible.