COMP 10024 – Lab: Mastering the Vi Editor

March 9, 2026 7:47 pm Published by

Vi is often the first “hurdle” for new UNIX users. Because it was designed before the mouse or modern cursor keys were standard, it forces you to think differently about how you interact with text. This lab will take you from a Vi novice to a confident editor.


Part 1: Entering the Matrix (The Modes)

The most important concept in Vi is Modes. You cannot simply start typing text the moment you open the program.

  1. Open a new file named practice.txt:

    vi practice.txt

  2. You are now in Command Mode. Try typing “Hello”.

    Observation: Nothing appears on the screen! In Command Mode, letters are instructions, not text.

  3. Press i. You are now in Insert Mode. Look at the bottom left of your screen; it should say -- INSERT --.
  4. Type the following sentence:

    “The quick brown fox jumps over the lazy dog.”

  5. Press Esc to return to Command Mode.

Part 2: Moving Without a Mouse

In Vi, we use the “home row” for navigation. While modern versions of Vi (Vim) allow arrow keys, true masters use h, j, k, and l.

  • h : Move Left
  • j : Move Down
  • k : Move Up
  • l : Move Right

Exercise: Move your cursor to the word “fox”.


Part 3: Basic Editing Commands

Ensure you are in Command Mode (press Esc just to be sure) before trying these commands.

Command Action
x Delete the single character under the cursor.
dd Delete (cut) the entire current line.
yy “Yank” (copy) the current line.
p Put (paste) the deleted/yanked text after the cursor.
u Undo the last action.

Task:

  1. Move to the start of the line and press yy.
  2. Move to the end of the line and press p five times. You should now have six identical lines.
  3. Go to the third line and press dd to delete it.

Part 4: The Power of Ex-Mode (Colon Commands)

Ex-Mode allows you to perform “big” actions like searching, replacing, and saving. You enter this mode by typing a colon (:) while in Command Mode.

  1. Turn on Line Numbers: Type :set number and press Enter.
  2. Search and Replace: Let’s change every instance of “fox” to “cat”. Type:

    :%s/fox/cat/g

    Breakdown: % means entire file, s is search, g means global (all instances on the line).


Part 5: The Great Escape (Saving and Quitting)

New users often get “trapped” in Vi. Here is how you get out:

  • :w — Write (Save) the file but stay in Vi.
  • :wq — Write and Quit (Save and exit).
  • :q! — Quit without saving (The “Panic Button”).

Final Task: Save your work and exit back to the shell using :wq.


Challenge: The Efficiency Test

Re-open your file and try to perform these “Jumps” from Command Mode:

  • Type G (capital) to jump to the very last line of the file.
  • Type 1G to jump back to the first line.
  • Type /cat to find the next occurrence of the word “cat”.

Categorised in: , ,

This post was written by amax

Comments are closed here.