Chapter 3. Vim Editor

In this chapter, we’ll explore Vim, a powerful text editor that is built into Linux systems. Vim is widely used for text editing in programming and system administration due to its speed, efficiency, and ability to handle large files with ease. Whether you’re new to Vim or looking to master its powerful features, this chapter will walk you through all the essential aspects of using Vim effectively in the Linux terminal.
What We Cover in This Chapter
The following topics are covered in this chapter:
What Is Vim and How to Launch It?
In this section, we’ll introduce Vim as an open-source Unix and Linux text editor. We’ll cover how to launch Vim, whether you’re working locally or remotely, and explore the basic differences between Vim and other text editors.
Normal, Insert and Visual Mode
Vim operates in three primary modes: Normal mode, Insert mode, and Visual mode. In this section, we'll explain each mode in detail, including how to switch between them and what actions are available in each mode. Understanding these modes is crucial for efficiently using Vim.
Normal Mode (1) – Move Cursor
In this section, we’ll explain how to navigate your file using the keyboard in Normal mode. You’ll learn how to use arrow keys and other shortcuts like k, j, h, and l to move the cursor. We’ll also cover how to jump to specific lines, move by words, and change pages.
Normal Mode (2) – Delete
Deleting text in Normal mode is simple and efficient. We’ll cover various ways to delete text, such as using the x key to delete a character, dd to delete a line, and how to delete multiple lines or text from a specific part of a line.
Normal Mode (3) – Copy and Paste
In this section, we'll show you how to copy (yank) and paste text in Normal mode. You’ll learn how to copy a line, multiple lines, and paste them at the desired location using the p or P command.
Normal Mode (4) – Undo and Redo
We’ll explain how to undo changes using the u command and redo them using Ctrl + r. Understanding undo and redo in Vim is key to quickly correcting mistakes without losing work.
Normal Mode (5) – Search Phrase
In this section, you’ll learn how to search for specific text within your document. We’ll cover how to use the / and ? commands to search forward and backward, and how to navigate through search results using n and N.
Normal Mode (6) – Replace Phrase
Learn how to replace specific phrases in your file. We’ll demonstrate the command for replacing a word or phrase with another, either across the entire document or within a specific line or section.
Normal Mode (7) – Save and Exit
Saving your work and exiting Vim are common tasks. In this section, you’ll learn how to use the :w
command to save changes, :q
to quit, and :wq
to both save and quit. We’ll also explain how to force quit without saving changes using :q!
.
Insert Mode
In Insert mode, you can freely add and delete text. We’ll explore the different ways to enter Insert mode and the various options available, such as using i to insert before the cursor or a to append after the cursor.
Visual Mode
Visual mode allows you to select text as a block for copying, deleting, or modifying. You’ll learn how to start Visual mode with v, select entire lines with V, and use the selected text for various actions like changing the case or copying it to another location.
Learn offline for better focus!
A book for this course is available on Amazon.
Linux Visual Guide
Step-By-Step Manual for Complete Beginners to Mastering Linux Basics, Vim Editor, Command Management, Remote Server Operation, and More.
Get the Book Now