COMP 10024 – Lab: Mastering the Vi Editor
March 9, 2026 7:47 pmVi 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.
- Open a new file named
practice.txt:vi practice.txt - You are now in Command Mode. Try typing “Hello”.
Observation: Nothing appears on the screen! In Command Mode, letters are instructions, not text.
- Press
i. You are now in Insert Mode. Look at the bottom left of your screen; it should say-- INSERT --. - Type the following sentence:
“The quick brown fox jumps over the lazy dog.”
- Press
Escto 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 Leftj: Move Downk: Move Upl: 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:
- Move to the start of the line and press
yy. - Move to the end of the line and press
pfive times. You should now have six identical lines. - Go to the third line and press
ddto 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.
- Turn on Line Numbers: Type
:set numberand press Enter. - Search and Replace: Let’s change every instance of “fox” to “cat”. Type:
:%s/fox/cat/gBreakdown:
%means entire file,sis search,gmeans 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
1Gto jump back to the first line. - Type
/catto find the next occurrence of the word “cat”.
Categorised in: COMP-10024, Lectures, Portfolio
This post was written by amax
Comments are closed here.