Master VI/Vim: Edit Like a Pro

Unlock the power of modal editing with this comprehensive guide

Why Learn VI/Vim?

VI (and its improved version Vim) is a powerful text editor available on almost all Unix-like systems. It's lightweight, efficient, and once mastered, can dramatically increase your text editing speed.

Unlike most editors, Vim uses modal editing with different modes for inserting text, navigating, and executing commands.

Basic Modes

Normal Mode (Command Mode)

Default mode for navigation and commands. Press Esc to return to Normal mode from any other mode.

Insert Mode

For inserting text. Enter with i, a, o, etc.

Visual Mode

For selecting text. Enter with v (character-wise), V (line-wise), or Ctrl+v (block-wise).

Command-Line Mode

For entering commands. Enter with :.

Essential Navigation

Basic Movement

h left, j down, k up, l right

w next word, b previous word

0 start of line, $ end of line

Screen Navigation

Ctrl+f page down

Ctrl+b page up

gg top of file

G bottom of file

Search Navigation

/pattern search forward

?pattern search backward

n next match

N previous match

Editing Commands

Inserting Text

i insert before cursor

a append after cursor

o new line below

O new line above

Changing Text

r replace character

cw change word

cc change line

c$ change to end of line

Deleting Text

x delete character

dw delete word

dd delete line

D delete to end of line

Copy & Paste

yy yank (copy) line

yw yank word

p paste after cursor

P paste before cursor

Advanced Techniques

Undo/Redo

u undo

Ctrl+r redo

Search & Replace

:%s/old/new/g replace all

:s/old/new/g replace in line

:n,ms/old/new/g replace between lines

Macros

qq start recording macro 'q'

q stop recording

@q execute macro 'q'

Multiple Files

:e file open file

:bn next buffer

:bp previous buffer

:bd close buffer

Windows & Splits

:sp horizontal split

:vsp vertical split

Ctrl+w then arrow key to navigate

Line Numbers

:set number show line numbers

:set relativenumber relative numbers

:set nonumber hide line numbers

VI vs Vim

While VI is the original editor, Vim (Vi Improved) adds many features:

Most commands work in both editors, but Vim offers many enhancements.

Practice Area

Try out some Vim commands in the text area below (note: this is a simulation, not actual Vim):

Pro Tip: Learning Strategy

1. Start by learning basic navigation (h, j, k, l) and how to switch between modes.

2. Practice daily for 15-30 minutes to build muscle memory.

3. Force yourself to use Vim for all text editing tasks.

4. Learn one new command each day and practice it until it becomes natural.

5. Customize your .vimrc file with settings that work for you.

Important Notes

• Vim has a steep learning curve but pays off in the long run.

• Don't try to learn everything at once - focus on the commands you use most often.

• If you get stuck, press Esc to return to Normal mode.

• To exit Vim, type :q to quit or :wq to save and quit.