Skip to main content

Command Palette

Search for a command to run...

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

Published
6 min read
Beginner-Friendly Guide to Vi & Vim: Powerful Command-Line Text Editors
J

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, and l to move around the text.

  • Insert Mode: This is where you add and edit text. To switch to Insert Mode, press i while 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 ESC to 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 ESC and 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 ESC to enter Command Mode, then type dd.

  • Delete Multiple Lines: To delete a block of text, press d followed by the number of lines you want to delete (e.g., d10 deletes 10 lines).

  • Undo a Mistake: Press U to 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., 10G jumps 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. Press n to jump to the next match.

  • Search Backwards: Press N to 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 character

  • j: Move down one line

  • k: Move up one line

  • l: Move right one character

  • 0: Move to the beginning of the line

  • $: Move to the end of the line

  • w: Move forward one word

  • b: Move backward one word

  • G: Move to the end of the file

  • gg: Move to the beginning of the file

More Advanced Moves

  • f: Find the next occurrence of a character (e.g., fx finds the next 'x')

  • t: Move to the character before the next occurrence of a character (e.g., tx moves to the character before the next 'x')

  • F: Find the previous occurrence of a character (e.g., Fx finds the previous 'x')

  • T: Move to the character after the previous occurrence of a character (e.g., Tx moves 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 cursor

  • dw: Delete the word under the cursor

  • dd: Delete the current line

  • d0: Delete from the cursor to the beginning of the line

  • d$: Delete from the cursor to the end of the line

Copying and Pasting

  • yy: Yank (copy) the current line

  • yw: Yank (copy) the current word

  • p: Paste after the cursor

  • P: Paste before the cursor

Changing Text

  • cw: Change the current word to Insert Mode

  • cc: Change the current line to Insert Mode

  • s: Substitute the character under the cursor


Undoing Your Mistakes: The Time-Turner

  • u: Undo the last change

  • Ctrl-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 selection

  • V: Enter visual line mode for line-wise selection

  • Ctrl-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!