
The vi editor is a classic, powerful text editor found on virtually every Linux and Unix system. While it might seem intimidating at first, mastering vi can significantly boost your productivity, especially when working in the terminal. This post will guide you through the basics, covering everything from starting vi to advanced text manipulation.
Starting Vi:
To open a file with vi, simply type vi filename in your terminal. If the file doesn't exist, vi will create it.
Bash
vi my_file.txt
Understanding Vi Modes:
vi operates in two primary modes:
Command Mode: This is the default mode. You use it to navigate the file, delete text, copy, paste, and execute other commands.
Insert Mode: You use this mode to insert and edit text.
Switching Modes:
To enter Insert Mode from Command Mode, press i (insert at cursor), a (append after cursor), o (open a new line below cursor), I (insert at beginning of line), A (append at end of line), or O (open a new line above cursor).
To return to Command Mode from Insert Mode, press the Esc key.
Inserting Text:
Open the file with vi.
Press i (or another insert command) to enter Insert Mode.
Type your text.
Press Esc to return to Command Mode.
Quitting Vi:
:q - Quit (if no changes have been made).
:wq - Save and quit.
:q! - Quit without saving changes.
:w - save changes without quiting.
Deleting Text:
x - Delete the character at the cursor.
dw - Delete a word.
dd - Delete the current line.
d$ - Delete to the end of the line.
d^ - delete to the beginning of the line.
ndd - delete n number of lines. (example: 3dd deletes 3 lines)
Copying and Moving Text:
yy - Copy (yank) the current line.
nyy- copy n number of lines. (example: 5yy copies 5 lines)
p - Paste (put) the copied text after the cursor.
P - Paste the copied text before the cursor.
dd - Delete (cut) the current line (also serves as a cut command).
ndd - Delete n number of lines.
To move a line, delete it (dd) and then paste it (p or P).
Searching Text:
/pattern - Search forward for the specified pattern.
?pattern - Search backward for the specified pattern.
n - Repeat the search (next occurrence).
N - Repeat the search in the reverse direction.
Replacing Text:
:s/old/new/ - Replace the first occurrence of "old" with "new" on the current line.
:s/old/new/g - Replace all occurrences of "old" with "new" on the current line.
:%s/old/new/g - Replace all occurrences1 of "old" with "new" in the entire file.
:%s/old/new/gc - Replace all occurrences of "old" with "new" in the entire file, with confirmation before each replacement.
Example: Replacing a Word
Open the file with vi.
Press Esc to enter Command Mode.
Type :%s/apple/orange/g and press Enter. This will replace all occurrences of "apple" with "orange" in the file.
Tips and Tricks:
Use u to undo the last change.
Use Ctrl + r to redo changes.
Use :set number to display line numbers.
Use :set nonumber to hide line numbers.
Practice regularly to build muscle memory.
Don't be afraid to experiment and explore the many other commands vi offers.
Let's put those vi commands into practice with a step-by-step exercise.
Exercise: Editing a Recipe File
Scenario: You have a plain text file containing a recipe for a simple cake. You need to make some edits to the ingredients and instructions.
Steps:
Create the Recipe File:
Open your terminal.
Create a new file called cake_recipe.txt using vi:
Bash
vi cake_recipe.txt
Press i to enter Insert Mode.
Paste the following recipe into the file:
Simple Cake Recipe Ingredients: - Apples (2) - Sugar (1 cup) - Flour (2 cups) - Milk (1/2 cup) - Eggs (3) Instructions: 1. Mix apples and sugar. 2. Add flour and milk. 3. Beat eggs and add to mixture. 4. Bake for 30 minutes.
Press Esc to return to Command Mode.
Save and exit the file: :wq and press Enter.
Edit the Ingredients:
Open cake_recipe.txt again with vi.
Move your cursor to the line containing "Apples (2)".
Press x repeatedly to delete "Apples" and type in "Bananas".
Move to the end of the line and change "(2)" to "(3)".
Move your cursor to the line containing "Milk (1/2 cup)".
Use dw to delete "1/2" and type in "1".
Move your cursor to the "Eggs (3)" line.
Use dd to delete the entire line.
Press o to create a new line below and add "Vanilla extract (1 tsp)".
Press Esc.
Edit the Instructions:
Move your cursor to the line containing "1. Mix apples and sugar."
Use d^ to delete "1. " and then type "1. Mix bananas and sugar.".
Move your cursor to the line containing "4. Bake for 30 minutes.".
Press A to go to the end of the line and add " at 350 degrees F.".
Move to the begining of the file.
Use /apples to search for "apples".
Use :%s/apples/bananas/g to replace all occurances of apples with bananas.
Press Esc.
Copy and Paste:
Move your cursor to the line containing "2. Add flour and milk.".
Press yy to copy the line.
Move your cursor to the end of the instructions.
Press p to paste the line.
Change the pasted line to "5. combine vanilla extract".
Save and Quit:
Press Esc to enter Command Mode.
Save the changes and exit vi: :wq and press Enter.
Verify Changes:
Use the cat command to view the contents of cake_recipe.txt:
Bash
cat cake_recipe.txt
You should now see the edited recipe with the updated ingredients and instructions.
This exercise covered:
Creating a file (vi filename)
Entering Insert Mode (i, a, o, A)
Deleting text (x, dw, dd, d^)
Copying and pasting (yy, p)
Searching (/pattern)
Replacing text (:s/old/new/g)
Saving and quitting (:wq)
Practice these commands regularly, and you'll become a proficient vi user!
Comments