Welcome to Module Four of your Introduction to Linux Level One course! In this module, we will learn about the vi Text Editor.
Imagine you are a writer. You need a notebook and a pen to write down your ideas. In Linux, the vi editor is like that notebook and pen. It is a tool that lets you create and edit text files right inside the terminal.
vi is one of the most powerful text editors in Linux. It is used by programmers, system administrators, and many other professionals. It might seem a little strange at first because it works differently from the editors you might be used to, but once you learn it, you will be able to edit files very quickly.
In this module, you will learn how to open vi, how to type text, how to save your work, and how to quit vi. You will also learn some basic navigation and editing commands.
By the end of this module, you will be comfortable using vi to create and edit text files. Let's begin our vi adventure!
By the time you finish this module, you will be able to:
Once upon a time, there was a magic typewriter that could only be used in a special way. It had two modes: command mode and writing mode.
In writing mode, you could type words freely, just like a normal typewriter. But to do anything elseβlike move to a new line, save your work, or fix a mistakeβ you had to switch to command mode.
At first, people were confused. They kept trying to type, but the typewriter wouldn't respond. They had to learn the special keys that let them switch between modes. Once they learned, they realized the typewriter was incredibly powerful. They could write faster and fix mistakes more easily than with any other typewriter.
The vi editor is exactly like that magic typewriter. It has command mode and insert mode. In insert mode, you type text. In command mode, you give commands to the editor. Once you learn how to switch between them, you can edit files very quickly and efficiently.
Let's learn how to use this magic editor!
Definition: vi is a text editor that runs in the terminal. It is used to create and edit text files.
Why it is important: vi is available on almost every Linux system. It is a powerful tool that many professionals use.
Simple explanation: vi is like a notebook for your computer. You can write anything you want in it.
Real-life example: Programmers use vi to write code. System administrators use vi to edit configuration files.
School example: A student uses vi to write an essay.
Home example: A person uses vi to write a shopping list.
Nigerian example: Nigerian developers use vi to write code.
Illustration:
vi EDITOR
|
+--- Text editor
|
+--- Runs in the terminal
|
+--- Powerful and fast
|
+--- Used by professionals
|
V
CREATE AND EDIT FILES
Mini summary: vi is a powerful text editor that runs in the terminal. It is used to create and edit text files.
Definition: You open vi by typing vi followed by the name of the file you want to edit.
Why it is important: You need to know how to open vi before you can use it.
Simple explanation: Opening vi is like opening a notebook to start writing.
Real-life example: Type vi notes.txt to open a file called "notes.txt".
School example: A student types vi essay.txt to open a file for their essay.
Home example: You type vi shopping.txt to open a file for your shopping list.
Nigerian example: A developer types vi index.html to open a web page file.
Illustration:
OPENING vi
user@computer:~$ vi filename.txt
(This opens filename.txt in vi)
Mini summary: You open vi by typing "vi" followed by the file name.
Definition: vi has two main modes: command mode and insert mode.
Why it is important: You need to understand the modes to use vi effectively. In insert mode, you type text. In command mode, you give commands.
Simple explanation: The two modes are like two sides of a magic pen. One side writes, and the other side erases and edits.
Real-life example: In command mode, you can move around, delete text, and save the file. In insert mode, you can type new text.
School example: A student uses insert mode to type their essay and command mode to fix mistakes.
Home example: You use insert mode to write your shopping list and command mode to delete an item.
Nigerian example: A developer uses command mode to navigate code and insert mode to write new code.
Illustration:
vi MODES
+-------------------+-------------------+
| Command Mode | Insert Mode |
+-------------------+-------------------+
| Move around | Type text |
| Delete text | Add new text |
| Save and quit | |
+-------------------+-------------------+
Mini summary: vi has two modes: command mode and insert mode. You type in insert mode and give commands in command mode.
Definition: Insert mode is the mode where you can type text.
Why it is important: You need to be in insert mode to add new text to a file.
Simple explanation: Insert mode is like writing with a pen. Whatever you type appears on the screen.
Real-life example: Press i to enter insert mode. Now you can type.
School example: A student presses "i" to start typing their essay.
Home example: You press "i" to start typing your shopping list.
Nigerian example: A developer presses "i" to start writing code.
Illustration:
ENTERING INSERT MODE
Press 'i' to enter insert mode
-- INSERT --
Now you can type your text
Mini summary: Insert mode is where you type text. Press "i" to enter insert mode.
Definition: Command mode is the mode where you give commands to the vi editor.
Why it is important: You need to be in command mode to save, quit, delete, and navigate.
Simple explanation: Command mode is like giving instructions to the editor. You tell it what to do.
Real-life example: Press Esc to return to command mode from insert mode.
School example: A student presses "Esc" to stop typing and give a command.
Home example: You press "Esc" to stop typing and save your file.
Nigerian example: A developer presses "Esc" to navigate code.
Illustration:
COMMAND MODE
Press 'Esc' to enter command mode
(Now you can give commands)
:w (save)
:q (quit)
dd (delete line)
Mini summary: Command mode is where you give commands. Press "Esc" to enter command mode.
Definition: You switch between modes using keys. Press "i" to go to insert mode. Press "Esc" to go to command mode.
Why it is important: You need to switch between modes to type and give commands.
Simple explanation: Switching modes is like changing tools. You use one tool to write and another to edit.
Real-life example: Press "i" to type. Press "Esc" to stop typing and give commands.
School example: A student switches between modes to type and edit their essay.
Home example: You switch between modes to write and save your shopping list.
Nigerian example: A developer switches between modes to write and navigate code.
Illustration:
SWITCHING MODES
Command Mode -------- i --------> Insert Mode
Insert Mode -------- Esc ------> Command Mode
Mini summary: Press "i" to go to insert mode. Press "Esc" to go to command mode.
Definition: To save and quit vi, you use commands in command mode.
Why it is important: You need to save your work before you close the file.
Simple explanation: Saving is like putting your notebook away after writing.
Real-life example: :w saves the file. :q quits vi. :wq saves and quits. :q! quits without saving.
School example: A student types ":wq" to save and quit their essay.
Home example: You type ":wq" to save and quit your shopping list.
Nigerian example: A developer types ":wq" to save and quit their code.
Illustration:
SAVING AND QUITTING
:w (save)
:q (quit)
:wq (save and quit)
:q! (quit without saving)
Mini summary: Use ":w" to save, ":q" to quit, ":wq" to save and quit, and ":q!" to quit without saving.
Definition: In command mode, you can move around the file using keys like h, j, k, l.
Why it is important: You need to move to different parts of the file to edit them.
Simple explanation: Navigation keys are like arrows that move the cursor around the page.
Real-life example: h moves left, j moves down, k moves up, l moves right.
School example: A student uses these keys to move around their essay.
Home example: You use these keys to move around your shopping list.
Nigerian example: A developer uses these keys to navigate code.
Illustration:
NAVIGATION KEYS
+-------------------+-------------------+
| Key | Action |
+-------------------+-------------------+
| h | Move left |
| j | Move down |
| k | Move up |
| l | Move right |
| 0 (zero) | Move to start of |
| | line |
| $ | Move to end of |
| | line |
| G | Go to end of file |
+-------------------+-------------------+
Mini summary: Use h, j, k, l to move around in command mode.
Definition: In command mode, you can delete, copy, and paste text using simple commands.
Why it is important: You need these commands to edit your text quickly.
Simple explanation: Editing commands are like erasers and scissors for your text.
Real-life example: x deletes the character under the cursor. dd deletes the entire line. dw deletes a word.
School example: A student uses "dd" to delete a line in their essay.
Home example: You use "x" to delete a character in your shopping list.
Nigerian example: A developer uses "dw" to delete a word in code.
Illustration:
EDITING COMMANDS
+-------------------+-------------------+
| Command | Action |
+-------------------+-------------------+
| x | Delete character |
| dd | Delete line |
| dw | Delete word |
| u | Undo |
| yy | Copy line (yank) |
| p | Paste |
+-------------------+-------------------+
Mini summary: Use commands like x, dd, dw, u, yy, and p to edit text.
Definition: u undoes the last change. Ctrl + r redoes a change.
Why it is important: Mistakes happen. Undo lets you fix them easily.
Simple explanation: Undo is like taking back a mistake. Redo is like putting it back.
Real-life example: Press u to undo a deletion. Press Ctrl + r to redo.
School example: A student uses "u" to undo a mistake in their essay.
Home example: You use "u" to undo a deletion in your shopping list.
Nigerian example: A developer uses "u" to undo a change in code.
Illustration:
UNDO AND REDO
u (undo)
Ctrl + r (redo)
Mini summary: Use "u" to undo changes and "Ctrl + r" to redo them.
Definition: yy copies (yanks) a line. p pastes the copied text.
Why it is important: Copying and pasting saves time when you need to repeat text.
Simple explanation: Copying and pasting is like using a photocopier for your text.
Real-life example: Press yy to copy a line. Move the cursor and press p to paste it.
School example: A student copies a line in their essay.
Home example: You copy a line in your shopping list.
Nigerian example: A developer copies a line of code.
Illustration:
COPY AND PASTE
yy (copy line)
p (paste below cursor)
P (paste above cursor)
Mini summary: Use "yy" to copy a line and "p" to paste it.
Definition: You can search for text in vi using the / command.
Why it is important: Searching helps you find specific words quickly in a large file.
Simple explanation: Searching is like using a magnifying glass to find something in a book.
Real-life example: Type /word to search for "word". Press n to go to the next match.
School example: A student searches for a specific word in their essay.
Home example: You search for an item in your shopping list.
Nigerian example: A developer searches for a variable name in code.
Illustration:
SEARCHING
/word (search for "word")
n (next match)
N (previous match)
Mini summary: Use "/" to search for text and "n" to find the next match.
Definition: You can replace text using the :s command.
Why it is important: Replacing helps you change words quickly without typing them again.
Simple explanation: Replacing is like using whiteout to change a word.
Real-life example: :s/old/new/g replaces all "old" with "new" in the current line.
School example: A student replaces a word in their essay.
Home example: You replace an item in your shopping list.
Nigerian example: A developer replaces a variable name in code.
Illustration:
REPLACING TEXT
:s/old/new/ (replace first "old" in line)
:s/old/new/g (replace all "old" in line)
:%s/old/new/g (replace all "old" in file)
Mini summary: Use ":s/old/new/" to replace text in vi.
Definition: Here are some tips and tricks to help you use vi more effectively.
Why it is important: These tips will make you faster and more efficient in vi.
Simple explanation: Tips are like shortcuts that make your work easier.
Real-life example: G goes to the end of the file. gg goes to the beginning. Ctrl + f moves forward one page. Ctrl + b moves backward one page.
School example: A student uses "G" to go to the end of their essay.
Home example: You use "gg" to go to the beginning of your shopping list.
Nigerian example: A developer uses "Ctrl + f" to scroll through code.
Illustration:
vi TIPS
G (go to end of file)
gg (go to beginning of file)
Ctrl + f (move forward one page)
Ctrl + b (move backward one page)
. (repeat last command)
Mini summary: Use G, gg, Ctrl + f, Ctrl + b, and . to work faster in vi.
Definition: Learning vi is a valuable skill that will help you in many Linux tasks.
Why it is important: vi is available on every Linux system. Knowing vi makes you a more capable Linux user.
Simple explanation: Learning vi is like learning to ride a bicycle. It might feel strange at first, but once you learn, you never forget.
Real-life example: Many professionals use vi every day.
School example: A student can use vi to edit files for school projects.
Home example: You can use vi to edit any text file.
Nigerian example: Nigerian developers use vi for coding and editing.
Illustration:
LEARNING vi
|
+--- Practice regularly
|
+--- Learn the commands
|
+--- Use it every day
|
+--- Become an expert
|
V
MASTER THE EDITOR
Mini summary: Anyone can learn vi with practice. It is a valuable skill for Linux users.
| Word | Simple Definition |
|---|---|
| vi | A powerful text editor for Linux. |
| Command Mode | The mode where you give commands. |
| Insert Mode | The mode where you type text. |
| Navigate | To move around in a file. |
| Delete | To remove text. |
| Undo | To reverse a change. |
| Redo | To reapply a change that was undone. |
| Yank | To copy text in vi. |
| Paste | To insert copied text. |
| Search | To find text in a file. |
+-------------------+-------------------+
| Command Mode | Insert Mode |
+-------------------+-------------------+
| Move around | Type text |
| Delete text | Add new text |
| Save and quit | |
+-------------------+-------------------+
Command Mode -------- i --------> Insert Mode
Insert Mode -------- Esc ------> Command Mode
+-------------------+-------------------+
| Key | Action |
+-------------------+-------------------+
| h | Move left |
| j | Move down |
| k | Move up |
| l | Move right |
| 0 (zero) | Move to start of |
| | line |
| $ | Move to end of |
| | line |
| G | Go to end of file |
+-------------------+-------------------+
+-------------------+-------------------+
| Command | Action |
+-------------------+-------------------+
| i | Enter insert mode |
| Esc | Enter command mode|
| :w | Save |
| :q | Quit |
| :wq | Save and quit |
| :q! | Quit without save |
| x | Delete character |
| dd | Delete line |
| dw | Delete word |
| u | Undo |
| yy | Copy line |
| p | Paste |
| /word | Search |
+-------------------+-------------------+
user@computer:~$ vi notes.txt
(Open file)
/searchword
(Search for "searchword")
n (next match)
N (previous match)
| vi | nano | gedit |
|---|---|---|
| Terminal-based | Terminal-based | Graphical |
| Very powerful | Simple | User-friendly |
| Steep learning curve | Easy to learn | Very easy |
| Available everywhere | Available everywhere | Not always available |
| Used by professionals | Used by beginners | Used by beginners |
| Command Mode | Insert Mode |
|---|---|
| Give commands | Type text |
| Navigate | Add new text |
| Delete | Edit text |
| Save/Quit | |
| Press "Esc" to enter | Press "i" to enter |
vi is a powerful text editor that runs in the terminal.
You open vi by typing "vi" followed by the file name.
vi has two modes: command mode and insert mode.
Insert mode is where you type text. Press "i" to enter.
Command mode is where you give commands. Press "Esc" to enter.
Press "i" to go to insert mode. Press "Esc" to go to command mode.
Use ":w" to save, ":q" to quit, ":wq" to save and quit.
Use h, j, k, l to navigate in command mode.
Use x, dd, dw, u, yy, p to edit text.
Use "u" to undo and "Ctrl + r" to redo.
Use "yy" to copy a line and "p" to paste it.
Use "/" to search for text and "n" to find the next match.
Use ":s/old/new/" to replace text.
Use G, gg, Ctrl + f, Ctrl + b, and . to work faster in vi.
Anyone can learn vi with practice. It is a valuable skill.
Congratulations! You have completed Module Four: The vi Text Editor!
You have learned what the vi text editor is and why it is used. You now understand the two modes: command mode and insert mode. You learned how to open vi, type text, save your work, and quit the editor.
You also learned how to navigate in vi, use basic editing commands, undo and redo changes, copy and paste text, search for text, and replace text. You also learned some tips and tricks to work faster in vi.
Remember, vi is a powerful tool that is used by many professionals. The more you practice, the more comfortable you will become. Don't be afraid to make mistakesβthat's how you learn!
In the next module, Module Five: User and Group Management, you will learn how to manage users and groups on a Linux system.
Match the command to its action:
| Command | Action |
|---|---|
| 1. i | A) Save |
| 2. Esc | B) Quit |
| 3. :w | C) Enter insert mode |
| 4. :q | D) Enter command mode |
| 5. :wq | E) Delete line |
| 6. dd | F) Save and quit |
| 7. u | G) Copy line |
| 8. yy | H) Undo |
Answers: 1-C, 2-D, 3-A, 4-B, 5-F, 6-E, 7-H, 8-G
Scenario 1: You need to create a new file called "notes.txt" and write a few sentences. What commands would you use?
Scenario 2: You made a mistake while typing and want to undo it. What command would you use?
Scenario 3: You want to delete a line in your file. What command would you use?
Scenario 4: You want to copy a line and paste it somewhere else. What commands would you use?
Activity: Create a vi Cheat Sheet.
Instructions:
Activity: Practice vi Editing.
Instructions:
Project: Create a vi Command Poster.
Instructions:
Assignment: vi Practice Report.
Instructions:
Challenge: The vi Challenge.
Instructions:
Congratulations on completing Module Four!
In the next module, Module Five: User and Group Management, you will learn how to manage users and groups on a Linux system. You will discover:
Get ready to manage users and groups!
End of Module Four π
vi is a powerful tool that every Linux user should learn!