A Beginner's Guide to Vim: The Elegant Path to Mastering Your Keyboard
It's said that programmers of the world can be divided into three groups: those who use Emacs, those who use Vim, and heretics.
This is a joke, of course, but it does highlight the status that Emacs and Vim hold in the minds of many (if you're interested in the love-hate relationship between them, you can check out the "Editor War"). I'm not skilled enough to have learned Emacs yet, but when it comes to Vim, I'd say I've gotten the hang of it.
Vim (short for Vi IMproved) is an enhancement of the legendary text editor vi. Many claim it's the best text editor ever made. I can't prove that, but I would argue that Vim is more than just a tool; it's a habit, a language. Vim embodies the philosophy of clean, efficient, and elegant text editing. Though it's small and simple, it can subtly boost productivity in a significant way.
For the differences between vi and Vim, you can check out their respective Wikipedia pages. In day-to-day use, there's not much distinction between vi and Vim. They share the same roots, with Vim being an improved version of vi, packed with useful features that make it more convenient. This article focuses on Vim.
Learning Vim is a process of building rules and then learning to break them. It starts with understanding commands and modes, but eventually, those complex commands fade into the background, transforming into a language and an intuitive habit. This article won't cover all of Vim's features; it's just an introduction. If you prefer, you can skip my ramblings and go straight to the recommended resources listed below.
In addition to the resources below, I highly recommend
vimtutor
, which comes bundled with Vim. Most distros
offer a package for vimtutor
, and you can also find
plenty of resources online if needed.
Resources for vi:
Resources for Vim:
- Vim Official Site
- Interactive Vim Tutorial (A great online Vim tutorial project, though still basic. It could use some contributions if you're interested.)
- Learn Vim Progressively
- A Vim Tutorial and Primer
- Operator, the True Power of Vim
Let's take a gradual approach to understanding Vim. If you're interested in specific sections, you can use the table of contents below to jump to the relevant part. I hope this guide proves useful.
Table of Contents
- Getting Started: Learning to Type
- Basics: What Else Can We Do
- After the Basics: Letting Vim Handle the Details
- Conclusion
Getting Started: Learning to Type
Before diving in, keep the following in mind:
-
Throughout the text, if a command involves pressing
Ctrl-λ
, I'll use<C-λ>
to represent it. (Hint: some tutorials may use^λ
to indicateCtrl-λ
for readability.) -
Commands starting with
:
require you to press<enter>
afterward. For example, when you see:q
, it means you should type:q<enter>
.
First, install Vim (the following is for Arch Linux):
$ sudo pacman -S vim
Once installed, create a new file:
$ vim hello
You've now entered Vim, but don't get too excited—you can't start editing just yet. That's because Vim operates in three different modes:
- Normal Mode
- Insert Mode
- Visual Mode
Each mode is indicated in the lower-left corner. In Normal Mode, there's no indication; this is the default mode when you start Vim. In this mode, you can enter commands like:
i // Enter Insert Mode
v // Enter Visual Mode
h // Move the cursor left
j // Move the cursor down
k // Move the cursor up
l // Move the cursor right
:w // Save the file (write)
:q // Quit the file
After entering Insert or Visual Mode, simply press ESC
to
return to Normal Mode. Additionally, while you can use the arrow keys
for navigation, I still recommend getting comfortable with using
hjkl
for movement. With this, you're now ready to use Vim
as a standard text editor.
Basics: What Else Can We Do
As you've seen, most commands are executed in Normal Mode, which is
why some people refer to it as Command Mode (though some experts
reserve this term, or Ex Mode, for commands entered with
:
—feel free to correct me if you have a more precise
definition). In Vim, commands can be thought of as a language. Once
you understand the meaning behind each key, you can combine them to
achieve various tasks. In this section, we'll explore these concepts
in more detail.
First, let's expand on mode-switching commands:
i // Insert text at the cursor position
a // Append text after the cursor
v // Visually select text character by character
V // Visually select text line by line
<C-v> // Visually select text in a block (visual block mode)
More Editing and Saving
Next, let's look at some basic editing commands:
d // Delete text
c // Change text
y // Yank (copy) text
The main difference between d
and c
is that
using c
puts you into Insert Mode afterward, hence the
term "change."
In addition to editing text, you'll need to know how to open and save files. Beyond the methods we've covered, you may also find these useful:
:q! // Force quit without saving changes
:wq // Save and quit
:w /some/path/[filename] // Save as a new file
:saveas /some/path/[filename] // Equivalent to the above command
:x // Quit without saving if there are no changes; otherwise, save and quit. It's a quicker version of :wq
ZZ // Another shortcut for :wq
Not Just Searching: Efficient Cursor Movement
Vim includes powerful search functionality:
t // Move the cursor to just before the specified character
f // Move the cursor to the specified character
In fact, T
and F
work similarly, but they
search backward instead. These search commands can be used not just
for finding text but also for moving the cursor, making text selection
more efficient. In addition to searching for single characters or
repeated patterns, Vim lets you search for arbitrary strings:
/ // Search forward for a string
? // Search backward for a string
When searching for a string, you can jump to the next or previous match:
n // Next match
N // Previous match
These keys behave differently depending on whether you searched forward or backward, so try them out to experience the difference. You might also notice changes in the command area in the lower-left corner.
Beyond these, there are two other flexible commands that blur the line between searching and moving:
* // Search for the word under the cursor (Vim intelligently detects what you're looking for, whether it's a word or a symbol)
% // Jump between matching pairs of brackets, parentheses, or braces ([, (, {)
Navigating Freely Within Text
Let's dive into more ways to move around:
0 // Move the cursor to the beginning of the line
$ // Move the cursor to the end of the line
^ // Move the cursor to the first non-blank character of the line
w // Move to the beginning of the next word
b // Move to the beginning of the previous word
e // Move to the end of the next word
W // Move to the next word (treating everything between spaces as a single word)
B // Move to the previous word (same principle as above)
You can also move in larger increments:
:line_number // Move to the specified line
H // Move to the top of the screen
M // Move to
the middle of the screen
L // Move to the bottom of the screen
[line_number]G // Go to the specified line (similar to using :)
gg // Go to the top of the file (equivalent to 1G)
G // Go to the bottom of the file
<C-U> // Move up half a screen
<C-D> // Move down half a screen
<C-F> // Move down one full screen (page forward)
<C-B> // Move up one full screen (page backward)
More Ways to Insert Content
I // Insert at the beginning of the line (before any whitespace)
A // Insert at the end of the line
o // Open a new line below the current one
O // Open a new line above the current one
r // Replace the character under the cursor
R // Replace characters starting from the cursor position
C // Change text from the cursor position to the end of the line
More Ways to Modify Content
In the world of Vim, you don't always need to switch to Insert Mode to modify text. Often, changes can be made directly in Normal Mode. Of course, it's worth noting that advanced users don't even think in terms of different modes, as I'll explain later.
x // Delete the character under the cursor
X // Delete the character before the cursor
dd // Delete the current line
D // Delete from the cursor to the end of the line
yy // Yank (copy) the current line (equivalent to dd followed by P)
J // Join the current line with the next line
One of Vim's conveniences is that whether you use
y
(yank), c
(change),
d
(delete), or x
(exterminate), Vim
automatically saves the affected content for easy pasting. Here's how
to paste:
p // Paste after the cursor
P // Paste before the cursor
Undoing and Redoing
Mistakes happen when editing, so you'll need to know how to undo and redo:
u // Undo the last action
<C-r> // Redo the last undone action
Using Vim as a Language
After learning the basics, we've only scratched the surface of Vim. The real mastery comes when you can use Vim as naturally as speaking a language. As you become more fluent, you'll notice that commands combine in different ways depending on their role as nouns or verbs. Here are a few examples to illustrate this:
i // Inside
a // Around
w // Word
s // Sentence
p // Paragraph
t // Tag (HTML/XML)
b // Block (in programming)
Now, let's look at some simple examples:
dw // Delete to the start of the next word
cb // Change to the start of the previous word
y2w // Yank (copy) the next two words
These are straightforward commands. For example, d
stands
for delete, and w
moves to the start of the next word, so
dw
means "delete to the next word." In the
command y2w
, you can see that adding a number before a
command repeats it that many times. This applies to almost all
commands and can significantly boost efficiency. Similarly, you can
use more flexible combinations:
0y$ // Yank (copy) from the beginning to the end of the line
y2/foo // Yank (copy) to the second occurrence of "foo"
Let's push the difficulty further:
ci" // Change the content inside the quotes (change in `"`)
da( // Delete the parentheses and everything inside (delete around `(`)
ct" // Change up to the next quote (change to `"`)
This might seem confusing—aren't i
and
a
normally used for inserting? Here, they're modifiers,
so their meaning changes.
Vim's magic lies beyond what's shown here. Once you grasp that Vim is a language, the possibilities are endless. You can freely combine commands to create new ones on the fly.
After the Basics: Letting Vim Handle the Details
Once you've practiced the above and deepened your understanding of Vim's language, you'll realize that mastering a language isn't the hard part; it's getting fluent that counts. Just as we aim to communicate clearly in our daily lives, with Vim, the more skilled you become, the more concise your commands will be. Often, Vim can handle tasks automatically for you. Here are some commonly used features:
Auto-Completion
Whether you're writing or coding, auto-completion is a powerful feature, and Vim's implementation is particularly strong. While in Insert Mode, you can use:
<C-p> // Auto-complete upward
<C-n> // Auto-complete downward
You might wonder why there are two auto-completion options. The reason
is efficiency: when handling large amounts of text, this distinction
can significantly speed up your workflow, reinforcing Vim's
high-efficiency design. Additionally, there are even more specific
auto-completion options triggered by <C-x>
. For
now, I'll keep things simple, but I may expand on this in a future
article.
Auto-Indentation
Auto-indentation is a feature that needs no introduction—it's often enough to make you fall in love with Vim:
= // Auto-indent
Just use <C-v>
to select the lines you want to
indent and press =
—your code will be beautifully aligned.
Of course, Vim also offers manual indentation:
< // Indent left
> // Indent right
When writing, auto-indentation might not always work as intended, so manual indentation can come in handy.
Conclusion
This article took quite some time to complete, mainly because I had many things going on during the writing process. So, I decided to publish it first and make revisions later. Beyond what I've introduced here, Vim has many more advanced features, like more sophisticated uses of Visual Mode or recording macros. Or, if you get really skilled, you might find that Visual Mode becomes unnecessary as you can accomplish everything with just a few keystrokes. There's so much more to discover in Vim. Progress is made step by step, and becoming proficient with Vim doesn't happen overnight. I hope this article encourages you to keep learning and improving.
(To Be Continued)