Skip to content

Editing with Vim

Module
Reading and Editing Text
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21; Ubuntu 24.04.4 LTS (course VM), Vim-tiny 9.1 via vi
Prerequisites
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.

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.

Terminal window
mkdir -p ~/lab/b07/vim
cd ~/lab/b07/vim
printf '# Text practice\ntopic=files\nstatus=draft\n' > demo.conf

The file is a practice configuration, not settings loaded by your system. The printf setup writes three lines to this disposable file, as in B04.

Terminal window
vi demo.conf
Vim-tiny shows the three original lines, a file message, and no Insert-mode banner.
vi opens demo.conf using the VM’s default configuration. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. The three original configuration lines are visible; color does not identify the active mode.
  2. 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.

Three editing modes and their keysNormal interprets editing keys. i or o enters Insert, and Esc returns to Normal. Colon or slash enters Command-line; Enter executes the command or search and Esc cancels it, returning to Normal for the examples shown. Other Vim modes and commands that exit the editor are omitted. NormalEditing keys InsertType file text Command-lineCommand or search i or o Esc : or / Enter / Esc Enter runs :w or a search here; :q instead leaves the editor.
Three editing modes and their keysNormal interprets editing keys. i or o enters Insert, and Esc returns to Normal. Colon or slash enters Command-line; Enter executes the command or search and Esc cancels it, returning to Normal for the examples shown. Other Vim modes and commands that exit the editor are omitted. NormalEditing keys InsertType file text Command-lineCommand or search i or o Esc : or / Enter / Esc Only three modes are shown.
Simplified model. Normal interprets editing keys, Insert accepts file text, and Command-line accepts a command or search.

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.

Press Esc, then type the following inside vi, including its colon:

Editor command — press Enter after typing
:set showmode
The bottom command line contains :set showmode before Enter.
Enable the mode display with an editor command. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. 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:

The unmodified file remains visible and -- INSERT -- appears at the bottom.
After set showmode, i makes Insert mode visible. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. 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.

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 buffer has a fourth line review=ready and the Insert banner.
o opens a line below the status and enters Insert mode. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. The added review=ready line is in the buffer.
  2. 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.

Type /review without Enter:

The bottom row reads /review before Enter, while review is highlighted in the buffer.
Enter a forward search from Normal mode. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. 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:

The cursor is on review and the bottom row reports search hit BOTTOM, continuing at TOP.
The search wraps and finds the review line. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. After Enter, the cursor lands on the r of review; the temporary search highlight is gone.
  2. 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.

From Normal mode, type :w:

The edited four-line file is visible with :w on its command line.
Prepare to save with :w. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. 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:

Terminal window
cat demo.conf
Captured output
# Text practice
topic=files
status=draft
review=ready
Bash prints the original three lines plus review=ready, followed by its prompt.
After saving and exiting, cat reads demo.conf. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. cat reads the four saved lines after vi exits.
  2. 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.

Reopen with the same plain command:

Terminal window
vi demo.conf

Press Esc, type 1G, press i, type TEMPORARY , then Esc. Try :q, Enter, without saving that disposable test change:

The buffer begins with TEMPORARY and the message is E37: No write since last change (add ! to override).
An unsaved edit makes :q refuse to exit. Ubuntu 24.04.4 LTS, course VM. Scroll inside the image on narrow screens.
  1. 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.

Terminal window
cat demo.conf
Captured output
# Text practice
topic=files
status=draft
review=ready

The previously saved file remains. Like Nano’s discard choice, :q! does not undo a write already made to disk.

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:

Terminal window
cd ~/lab/b07/vim
ls -l .demo.conf.swp
Terminal window
rm .demo.conf.swp
vi demo.conf

Goal: Change a value in a separate handoff while preserving the saved demonstration file.

Terminal window
cp demo.conf handoff.conf
vi handoff.conf

Find 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
  1. Esc, 1G, /draft, Enter.
  2. cw, type checked, Esc.
  3. :w, Enter, then :q, Enter.
Terminal window
cat handoff.conf
Captured output
# Text practice
topic=files
status=checked
review=ready

This saved handoff was captured on the course VM. The source demo.conf still has status=draft.

Close this lesson’s editor first, saving or discarding deliberately. Inspect the directory from its parent:

Terminal window
cd ~/lab/b07
ls -AR vim

After the challenge it contains demo.conf and handoff.conf. Do not remove an editor’s working directory while its session is still open.

Terminal window
rm -r ~/lab/b07/vim

Rerun 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 vi runs 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 showmode makes Insert visible in this session.
  • :w writes, :q quits, 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.

  • On your system: man vi; the VM’s vi --version and /etc/vim/vimrc.tiny identify 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 showmode and compatible.