Linux VI Editor
May 22, 2025
The VI editor (and its enhanced cousin, Vim – Vi Improved) is undeniably one of them. While graphical text editors offer comfort, VI is the bedrock of text manipulation in the Linux command-line environment. It’s ubiquitous, lightning-fast once mastered, and incredibly powerful. My first encounter with VI was, to be frank, frustrating.
Table of Contents
- Table of Contents
- Understanding VI’s Core Philosophy
- Mastering Movement
- Entering Insert Mode
- Editing Text
- Search and Replace
- Working with Files
Understanding VI’s Core Philosophy
The biggest hurdle for newcomers to VI is its modal nature. Unlike most modern text editors where you type directly, VI operates in distinct modes. This was the concept that initially stumped me, but it’s also the secret to its efficiency. Once you grasp these modes, VI transforms from a cryptic puzzle into a fluid, intuitive tool.
1. Normal Mode (Command Mode)
When you first open a file in VI, you are in Normal Mode. This is the default. My initial mistake was trying to type immediately, only to find random characters appearing or the cursor moving unexpectedly. Normal Mode is not for typing text. It’s for:
- Navigation: Moving the cursor around the file.
- Command Execution: Performing actions like deleting lines, copying text, pasting, undoing, searching, and initiating other modes.
Think of Normal Mode as your control center. All the powerful manipulation happens from here. You press Esc
to return to Normal Mode from any other mode. This Esc
key is your most frequent companion in VI.
2. Insert Mode
This is the mode where you actually type text. To enter Insert Mode from Normal Mode, you typically press i
. Once in Insert Mode, VI behaves much like any other text editor. You can type, backspace, and new text will be inserted.
3. Command-Line Mode (Ex Mode)
From Normal Mode, when you press :
(colon), you enter Command-Line Mode. This mode allows you to execute powerful, single-line commands, often related to file saving, quitting, searching, and global replacements. The command prompt appears at the bottom of the screen.
Understanding these three modes is the fundamental key to unlocking VI’s power. It took me a while, but once it clicked, my efficiency soared.
Mastering Movement
One of the first things that struck me about VI was its reliance on the h
, j
, k
, l
keys for cursor movement, rather than the arrow keys. This design choice, originating from old ADM-3A terminals that lacked dedicated arrow keys, keeps your fingers on the home row, minimizing hand movement and maximizing speed. It felt awkward at first, but now it’s second nature.
h
: Move left (like left arrow)l
: Move right (like right arrow)j
: Move down (like down arrow)k
: Move up (like up arrow)
While we eventually learn to use arrow keys, training myself on h
, j
, k
, l
dramatically improved my speed in VI.
Navigating Within a Line
Beyond single-character moves, VI offers incredibly efficient ways to jump around a line:
0
(zero): Move to the beginning of the current line. I use this constantly.^
: Move to the first non-blank character of the current line. Often more useful than0
when dealing with indented code or configuration files.$
: Move to the end of the current line. Another frequent go-to for me.w
: Move to the next word. Great for quickly jumping over code or text.b
: Move to the previous word. The counterpart tow
.e
: Move to the end of the current word.
Navigating the Entire File
For larger files, line-by-line movement is too slow. VI offers powerful commands for rapid navigation:
gg
: Move to the start of the file (first line, first character).G
: Move to the end of the file (last line, first character).:n
: Move to line numbern
. For example,:100
takes you to the 100th line. This is invaluable when debugging stack traces or error logs that reference line numbers.
we find ourselves constantly combining these navigation commands with other operations. For instance, G
followed by A
will take me to the end of the file and immediately put me into insert mode to append new content.
Entering Insert Mode
To start typing, you need to be in Insert Mode. VI provides several ways to enter it from Normal Mode, each offering a slightly different starting point:
i
: Insert before the cursor. (Most common)I
: Insert at the beginning of the current line. (Very useful for adding comments or new lines at the start)a
: Append after the cursor.A
: Append at the end of the current line. (Excellent for adding content to the end of a line)o
: Open a new line below the current line and enter insert mode.O
: Open a new line above the current line and enter insert mode.
After typing, remember Esc
to return to Normal Mode. This constant switching between modes is the core rhythm of VI.
Editing Text
Once you’re comfortable with navigation and inserting text, the real power of VI emerges with its editing commands. These commands are executed from Normal Mode.
Deletion: Precision and Speed
x
: Delete the character under the cursor. (Like pressing Delete key)X
: Delete the character before the cursor. (Like pressing Backspace key)dw
: Delete a word. (Delete word from cursor to end of word)dd
: Delete the entire line. This is one of my most used commands.d$
: Delete from the cursor to the end of the line.d0
: Delete from the cursor to the beginning of the line.D
: Equivalent tod$
. (Delete to end of line from cursor)
The power of these deletion commands comes from their ability to be combined with numbers. For example, 5dd
deletes 5 lines, and 3dw
deletes 3 words. This concept of combining a number with an action is a cornerstone of VI’s efficiency.
Undo and Redo
Mistakes happen, especially when you’re rapidly editing. VI’s undo/redo functionality is robust.
u
: Undo the last action. we should pressedu
to revert an accidental deletion.Ctrl + r
: Redo an undone change.
Copy and Paste (Yank and Put)
In VI, copying is called “yanking,” and pasting is called “putting.”
yy
: Copy (yank) the entire line. (Another highly frequent command for me)yw
: Copy (yank) the current word.p
: Paste after the cursor (or below the current line if you yanked a whole line).P
: Paste before the cursor (or above the current line if you yanked a whole line).
Again, you can prefix yy
with a number, e.g., 5yy
to copy 5 lines. This command-verb-object syntax is what makes VI so efficient: number + action + motion.
Search and Replace
Searching and replacing text are common tasks, and VI provides powerful ways to do them. These are typically executed from Command-Line Mode.
Searching
/pattern
: Search forward from the current cursor position forpattern
. As you type the pattern, VI often highlights matches in real-time.?pattern
: Search backward from the current cursor position forpattern
.n
: Repeat the last search forward.N
: Repeat the last search backward.
These search commands are incredibly useful for quickly locating specific lines or phrases in large files.
Replacing
Replace operations are performed in Command-Line Mode. The syntax is a bit more involved but immensely powerful.
:%s/old/new/g
: This is the most common and powerful replacement command. It replaces all occurrences of “old” with “new” throughout the entire file.%
: Specifies the entire file.s
: Stands for “substitute.”/old/new/
: The pattern to find (old
) and the replacement string (new
).g
: Global flag, meaning replace all occurrences on each line (withoutg
, it only replaces the first occurrence on a line).
:s/old/new/g
: Replaces all occurrences of “old” with “new” only on the current line.:%s/old/new/gc
: Addsc
for “confirm.” This prompts you for confirmation before each replacement. Invaluable when you want to review changes before committing.
we will use these replacement commands countless times for configuration file adjustments, code refactoring, and data manipulation.
Working with Files
Managing files themselves is also done from Command-Line Mode.
:w
: Save the current file (write changes to disk).:wq
: Save and exit the current file. (Equivalent toZZ
in Normal Mode).:q
: Quit VI. This will fail if there are unsaved changes.:q!
: Quit without saving (force quit). Use this when you want to discard all changes since the last save.:e filename
: Open a new file in the current window. If you have unsaved changes in the current file, you’ll be prompted to save or force quit (:e! filename
).
Multi-File Editing
One of VI’s more advanced, but incredibly useful, features is the ability to split the screen and work on multiple files simultaneously. This is a game-changer for comparing files or referencing one while editing another.
:split filename
: Split the screen horizontally and openfilename
in the new lower pane.:vsplit filename
: Split the screen vertically and openfilename
in the new right pane.Ctrl + w + w
: Switch between split screens (windows). You can also useCtrl + w + h
(left),Ctrl + w + l
(right),Ctrl + w + j
(down),Ctrl + w + k
(up).:only
: Close all other split windows, leaving only the current one.:qall
: Quit all open files/windows.
Using split screens drastically improves my productivity when dealing with related configuration files or comparing code versions.