In Part 1, we covered the basics of opening vim, understanding normal and insert modes, and moving around with the hjkl keys. If you haven't read that yet, I'd recommend starting there as we'll build on those concepts.
Now it's time to tackle what might be the most infamous aspect of vim: how to actually save and exit the program. We'll also dive into some useful editing commands that will start to show you why vim users are so passionate about this editor.
Let's address the elephant in the room first. The joke about developers being trapped in vim is real - it happened to me the first time I opened it. The secret? Command mode.
Command mode is accessed by pressing : (colon) while in normal mode. You'll see the colon appear at the bottom left of your terminal. This is where you tell vim to perform actions like saving, exiting, searching, and replacing text.
Here are the essential commands you need to know:
Saving and Exiting:
:w " Write (save) the file
:q " Quit vim
:wq " Write and quit (save then exit)
:q! " Quit without saving (force quit)
:x " Save and exit (only writes if changes were made)
Note: The ! (bang) is used in vim to force an action. You'll see this pattern throughout vim commands.
Let's practice:
Pro tip: If you try to quit without saving changes, vim will warn you. This has saved me countless times from accidentally losing work.
In Part 1, we covered i, a, and o. Here are a few more insert commands that will speed up your workflow:
i " Insert before cursor (you know this one)
a " Append after cursor (you know this one too)
o " Open new line below and insert
O " Open new line above and insert (capital O)
I " Insert at the beginning of the line
A " Append at the end of the line
The uppercase versions (I and A) are incredibly useful. Instead of pressing 0 to go to the start of a line then i to insert, just press I. Same with A for the end of the line - no more holding down l or using $ then a.
This is where vim starts to shine. In normal mode, you can delete text without ever entering insert mode:
Basic Deletion:
x " Delete character under cursor
X " Delete character before cursor (backspace)
dd " Delete entire line
D " Delete from cursor to end of line
The dd command is one you'll use constantly. Need to remove a line? Just hit dd - no selecting, no holding delete.
Changing Text:
cw " Change word (deletes word and enters insert mode)
cc " Change entire line (deletes line and enters insert mode)
C " Change from cursor to end of line
The c (change) command is powerful because it combines deletion and insertion. Try this:
In vim terminology, copying is called "yanking":
yy " Yank (copy) entire line
yw " Yank word
p " Paste after cursor
P " Paste before cursor (capital P)
Here's a common workflow:
Important: When you use dd to delete a line, it's actually cut (not just deleted). You can paste it elsewhere with p.
Made a mistake? Vim has you covered:
u " Undo last change
Ctrl+r " Redo
The u key is your best friend. I probably hit it a dozen times while writing this article. You can undo multiple times by pressing u repeatedly.
In Part 1, we covered hjkl. Now let's add some turbo boosters:
Word Movement:
w " Move forward one word
b " Move backward one word
e " Move to end of word
Line Movement:
0 " Move to beginning of line
$ " Move to end of line
^ " Move to first non-whitespace character
File Movement:
gg " Go to top of file
G " Go to bottom of file (capital G)
:42 " Go to line 42 (or any line number)
These movement commands are game-changers. Instead of holding j for 20 lines, just type :20 and press enter.
Here's where vim gets really powerful - you can combine numbers with commands:
3dd " Delete 3 lines
5j " Move down 5 lines
4yy " Yank 4 lines
2w " Move forward 2 words
This is the vim "language" - a number, an action, and a motion. Once you understand this pattern, you can create your own combinations.
Let me show you a typical editing session:
$ vim example.txt
One more useful command for Part 2:
/searchterm " Search forward for "searchterm"
?searchterm " Search backward for "searchterm"
n " Next occurrence
N " Previous occurrence (capital N)
After typing / or ?, type your search term and press enter. Use n to jump to the next match and N to go back.
Let's put it all together. Create a new file called practice.txt:
$ vim practice.txt
Now try this sequence:
If you can do all of that without looking back at the commands, you're well on your way to vim proficiency!
When I first started using vim back in 2011, I thought it was overly complicated. Why would anyone want to memorize all these commands when you could just use a mouse and menu? But after forcing myself to use it for a week on my home server, something clicked. I found myself getting frustrated when I had to use other editors because they felt slow.
The beauty of vim is that it becomes muscle memory. You stop thinking "I need to delete three lines" and just type 3dd without conscious thought. Your fingers know what to do.
We've covered a lot in this article - saving and exiting, editing commands, movement, and some basic vim "grammar." These commands will handle probably 80% of your daily editing needs.
In Part 3, we'll dive into visual mode, text objects (which will blow your mind), macros, and some of my favorite productivity tricks. We'll also touch on basic .vimrc configuration so you can start customizing vim to your preferences.
Until then, I highly recommend just using vim for everything. Edit your configs with it, write notes with it, even use it for this practice. The only way to really learn vim is to use it daily. Trust me, within a week you'll be hooked.
Here's a cheat sheet of everything we covered:
Save/Exit:
Insert:
Delete/Change:
Copy/Paste:
Movement:
Other:
Happy vimming!