Skip to content

Linux Lab 9: VI Editor

This lab is a hands-on introduction to vi (or vim), the ubiquitous text editor found on nearly every Linux system. Mastering vi is essential for system administration, quick edits, and working in environments without a graphical interface.

Table of Contents


Key Concepts

  • vi / vim: A modal text editor. "Modal" means the same keys do different things depending on which "mode" you are in.
  • Command Mode: The default mode. Keys are used for navigation (h, j, k, l), copying, deleting, and pasting. You cannot type text directly into the file here.
  • Insert Mode: The mode for typing text. You enter this mode by pressing i, a, or o.
  • Last Line Mode (Ex Mode): Accessed by pressing :, this allows you to save (w), quit (q), search, and perform other file-level operations.

Step-by-Step Walkthrough

1. Identifying the Default Editor

Question: What is the default editor configured in the lab? Answer: VIM

Explanation: The command update-alternatives --display editor shows the system's configured default. The output /usr/bin/vim.basic indicates that Vim (Vi IMproved) is the active editor.

2. Opening Files

Question: What is the command to open a file? Answer: vi or vim

Explanation: Running vi filename.txt opens the file in the editor. If the file doesn't exist, vi creates a new buffer for it.

3. Understanding VI Modes

Question: What are the operation modes in vi editor? Answer: Command mode, Insert mode, Last Line mode

Explanation: These are the three primary states: * Command: For navigation and manipulation. * Insert: For typing. * Last Line: For saving, quitting, and settings.

4. The Default Mode

Question: What is the default mode after opening a file? Answer: Command Mode

Explanation: When you first launch vi, you are always in Command Mode. You cannot start typing text immediately; you must first press a command (like i) to switch modes.

5. Switching Modes

Question: Identify the WRONG statement about switching modes. Answer: "Press capital letter T to switch to insert mode from command mode"

Explanation: * i: Switch to Insert Mode (insert before cursor). * I: Switch to Insert Mode (insert at beginning of line). * a: Switch to Insert Mode (append after cursor). * A: Switch to Insert Mode (append at end of line). * T: This is a navigation command ("till" character backward), not a mode switch.

6. Saving and Exiting

Question: Identify the correct exit commands. Answer: All are correct.

Explanation: * :w: Write (save) the file but stay open. * :q!: Quit without saving (force quit). * :wq: Write and Quit (save and exit).

7. Editing Task: Insert Mode

Task: Create kodekloud.txt with "Hello world!". Steps: 1. vi kodekloud.txt (Opens in Command Mode) 2. Press i (Enters Insert Mode) 3. Type Hello world! 4. Press Esc (Returns to Command Mode) 5. Type :wq! (Saves and exits)

8. Copying Lines (Yanking)

Question: What is the command to copy a line? Answer: yy

Explanation: In vi/vim terminology, copying is called yanking. * y: Yank. * yy: Yank the current line.

9. Deleting and Pasting

Question: Identify the WRONG statement. Answer: "dd - Highlight a line"

Explanation: * yy: Copy (Yank) a line. (Correct) * p: Paste a line. (Correct) * x: Delete a character. (Correct) * dd: This deletes (cuts) the current line. It does not highlight it. Visual mode (v or V) is used for highlighting.

10. Undo and Redo

Question: Match the correct undo/redo actions. Answer: A (u - undo) and B (ctrl+r - redo)

Explanation: * u: Undo the last change. * Ctrl + r: Redo the change that was undone.

11. Editing Task: Move a Line

Task: Move line 9 to line 5 in /home/bob/testfile.txt. Steps: 1. vi /home/bob/testfile.txt 2. Type :set nu (Optional: shows line numbers) 3. Type 9G or use arrow keys to go to line 9. 4. Type dd (Deletes/Cuts the line "you found me"). 5. Type 5G or move to line 4 ("Today is a great day..."). 6. Type p (Pastes the cut line below the current line, making it the new line 5). 7. :wq

12. Editing Task: Delete Multiple Lines

Task: Delete the first three lines. Steps: 1. Move cursor to the first line (gg or 1G). 2. Type d3d. * d: Delete operator. * 3: Count (perform action 3 times). * d: Operates on lines. * Result: Deletes current line + next 2 lines. 3. :wq


Command Reference

Key Mode Action
i Command Enter Insert mode (before cursor)
a Command Enter Insert mode (Append after cursor)
Esc Insert Return to Command mode
h, j, k, l Command Navigation: Left, Down, Up, Right
x Command Delete character under cursor
dd Command Delete (Cut) current line
d[N]d Command Delete [N] lines (e.g., d3d)
yy Command Copy (Yank) current line
p Command Paste below cursor
u Command Undo last change
Ctrl+r Command Redo
:w Last Line Save (Write) file
:q! Last Line Quit without saving (Force)
:wq Last Line Save and Quit
:set nu Last Line Show line numbers