Vim Basics: Part 2

Vim Basics: Part 2

Welcome Back

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.

The Infamous Exit Problem

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

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:

  1. Open your greetings.txt file: vim greetings.txt
  2. Make a change in insert mode (i then type something)
  3. Press Esc to return to normal mode
  4. Type :w and press Enter - you’ve just saved!
  5. Type :q and press Enter - you’ve exited!

Pro tip: If you try to quit without saving changes, vim will warn you. This has saved me countless times from accidentally losing work.

More Ways to Enter Insert Mode

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.

Deleting and Changing Text

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:

  1. Open a file with some text
  2. Place your cursor on a word
  3. Type cw
  4. Notice the word is deleted and you’re in insert mode
  5. Type a new word, then Esc

Copy, Cut, and Paste

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:

  1. Navigate to a line you want to copy
  2. Type yy to yank it
  3. Move to where you want to paste
  4. Type p to paste

Important: When you use dd to delete a line, it’s actually cut (not just deleted). You can paste it elsewhere with p.

Undo and Redo

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.

Moving Around Faster

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.

Combining Commands Like a Pro

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.

A Real-World Example

Let me show you a typical editing session:

$ vim example.txt
  1. i - enter insert mode
  2. Type some text
  3. Esc - back to normal
  4. yy - copy the line
  5. p - paste it
  6. dd - delete a line you don’t need
  7. :w - save
  8. Continue editing…
  9. u - oops, undo that
  10. :wq - save and exit

Searching Text

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.

Practice Exercise

Let’s put it all together. Create a new file called practice.txt:

$ vim practice.txt

Now try this sequence:

  1. Press i and type: “The quick brown fox jumps over the lazy dog”
  2. Press Esc
  3. Press o and type another sentence
  4. Press Esc
  5. Type :w to save
  6. Type gg to go to the top
  7. Type yy to copy the first line
  8. Type G to go to the bottom
  9. Type p to paste
  10. Type dd to delete a line
  11. Type u to undo
  12. Type :wq to save and quit

If you can do all of that without looking back at the commands, you’re well on your way to vim proficiency!

My Thoughts

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.

What’s Next?

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.

Quick Reference

Here’s a cheat sheet of everything we covered:

Save/Exit:

  • :w - save
  • :q - quit
  • :wq or :x - save and quit
  • :q! - quit without saving

Insert:

  • i - insert before cursor
  • a - append after cursor
  • o - open line below
  • O - open line above
  • I - insert at line start
  • A - append at line end

Delete/Change:

  • x - delete character
  • dd - delete line
  • cw - change word
  • cc - change line

Copy/Paste:

  • yy - yank line
  • p - paste after
  • P - paste before

Movement:

  • hjkl - left, down, up, right
  • w/b - word forward/backward
  • 0/$ - line start/end
  • gg/G - file top/bottom

Other:

  • u - undo
  • Ctrl+r - redo
  • /text - search forward
  • n/N - next/previous match

Happy vimming!