Beginner-Friendly Guide to Vi & Vim: Powerful Command-Line Text Editors

I'm a software developer from Nigeria with a passion for documentation. I love to help others solve problems and gain more knowledge in matters related to tech development.
Think of Your Computer as a Toolbox
Your computer is like a toolbox, filled with various tools to help you accomplish different tasks. The graphical interface (GUI) is like a workbench with clearly labeled drawers, making it easy to find the tools you use most often. But sometimes, you need to reach deeper for a specific tool—that’s where the command line (CLI) comes in. The CLI is a text-based interface that allows you to control your computer using powerful commands.
Vim: Your Super-Fast Text Editor
Imagine a text editor built for the CLI that's lightning-quick and precise. That's Vim! Vim is ideal for programmers, system administrators, and anyone who needs to get things done efficiently. It allows you to edit text files directly in the command line, offering speed and flexibility.
Why Use Vim?
Speedy Edits: Need to tweak a file quickly? Vim lets you do it right in the command line, saving you time by avoiding the need to switch between different programs.
All-in-One Powerhouse: With Vim, you can create, edit, and save files—all within a single tool. This helps you stay focused on your task.
Works with Many File Types: From basic text to complex scripts, Vim can handle a wide variety of file formats.
No Fancy Interface, No Problem: On systems without a graphical interface (like servers), Vim is your go-to editor.
Plays Well with Others: Tools like Git, used for code management, often rely on Vim for editing commit messages.
Getting Started with Vim
Installation
Most Unix-like systems (such as Linux) already have Vim pre-installed. If it’s not installed on your system, you can add it using the following command in your terminal:
Plain Text
sudo apt install vim
Opening Files
To open a file, navigate to its directory in your terminal and type:
Plain Text
vim [filename]
Example: To open a file named "Readme.md":
Plain Text
vim Readme.md
Think of Vim Like a Two-Sided Coin
Vim operates in two primary modes:
Command Mode (Default): This is where you navigate, search, delete, and undo changes. Think of it as the "control center" for Vim. Use keys like
h,j,k, andlto move around the text.Insert Mode: This is where you add and edit text. To switch to Insert Mode, press
iwhile in Command Mode.
Saving and Exiting
When you’re finished editing, here’s how to save your work and exit Vim:
Save and Quit: Press
ESCto switch to Command Mode, then type:wq. This command saves your changes and exits Vim.Discard Changes: If you want to exit without saving changes, press
ESCand type:q!.
Creating New Files
To start a new file, specify the filename when opening Vim:
Plain Text
vim config.yaml
Essential Editing Tricks
Here are some basic commands to help you get started with Vim:
Delete a Line: Press
ESCto enter Command Mode, then typedd.Delete Multiple Lines: To delete a block of text, press
dfollowed by the number of lines you want to delete (e.g.,d10deletes 10 lines).Undo a Mistake: Press
Uto undo your last change.
Jump Around
Navigate your text efficiently with these commands:
Jump to the end of the current line and start editing: Press
A.Move to the end of the line without switching modes: Press
$.Jump to the beginning of the line: Press
O.Jump directly to a specific line: Type the line number followed by
G(e.g.,10Gjumps to line 10).
Finding Things Fast
Lost in a sea of text? Use Vim’s search capabilities:
Search for a Text: Press
/followed by the word or phrase you’re looking for. Pressnto jump to the next match.Search Backwards: Press
Nto search in reverse.
Replace Everything at Once
To replace every instance of a word in your file, use the following command:
Plain Text
%s/old_string/new_string/g
Example: To replace all occurrences of "nginx" with "web-app":
Plain Text
%s/nginx/web-app/g
Mastering Vim: A Deeper Dive
Now that you have the basics, let’s dive deeper into Vim’s world and unlock its full potential. Think of Vim as a martial art; with practice, you’ll become a text-editing ninja.
Navigating Your Text Battlefield
Here are essential navigation commands:
Basic Movements
h: Move left one characterj: Move down one linek: Move up one linel: Move right one character0: Move to the beginning of the line$: Move to the end of the linew: Move forward one wordb: Move backward one wordG: Move to the end of the filegg: Move to the beginning of the file
More Advanced Moves
f: Find the next occurrence of a character (e.g.,fxfinds the next 'x')t: Move to the character before the next occurrence of a character (e.g.,txmoves to the character before the next 'x')F: Find the previous occurrence of a character (e.g.,Fxfinds the previous 'x')T: Move to the character after the previous occurrence of a character (e.g.,Txmoves to the character after the previous 'x')
Text Manipulation: The Ninja’s Arsenal
Vim offers powerful tools for text manipulation:
Deleting Text
x: Delete the character under the cursordw: Delete the word under the cursordd: Delete the current lined0: Delete from the cursor to the beginning of the lined$: Delete from the cursor to the end of the line
Copying and Pasting
yy: Yank (copy) the current lineyw: Yank (copy) the current wordp: Paste after the cursorP: Paste before the cursor
Changing Text
cw: Change the current word to Insert Modecc: Change the current line to Insert Modes: Substitute the character under the cursor
Undoing Your Mistakes: The Time-Turner
u: Undo the last changeCtrl-r: Redo the last undo
Saving and Exiting: Mission Complete
:w: Save the file:q: Quit Vim:wq: Save and quit Vim:q!: Quit Vim without saving
Visual Mode: Selecting Your Target
v: Enter visual mode for character-wise selectionV: Enter visual line mode for line-wise selectionCtrl-v: Enter visual block mode for column-wise selection
Additional Tips
Practice Regularly: The more you use Vim, the better you'll become.
Explore Vim Tutorials: There are countless online resources to help you learn more.
Customize Vim: Vim is highly configurable; you can tailor it to your preferences.
With this guide, you're now equipped to take on the command-line world with Vim. Keep practicing, and soon you'll become efficient in editing text in vim like a pro!

