The Complete Guide to the Vi Editor

Any developer venturing into the terminal environment for the first time will eventually face a stark black screen with a lone blinking cursor and an unfamiliar editor named 'Vi' or 'Vim'. For those of us accustomed to intuitive graphical user interfaces (GUIs), the first impression of Vi is nothing short of bewildering. The mouse is useless, keystrokes don't input text, and then suddenly, entire lines vanish without warning. This is why many are tempted to type :q!
and make a hasty escape.
However, Vi is pre-installed on virtually every UNIX-based system (including macOS and Linux), making it a tool you can encounter anywhere. Once you get the hang of it, it offers a level of productivity that few other editors can match. It allows you to experience a state of flow, editing and modifying code at the speed of thought without ever taking your hands off the keyboard.
I'm Alex, your IT journalist, and I'll be your guide on this Vi journey. Through this article, you will understand Vi's core philosophy, master its essential functions, and become a true master of your terminal environment.
The biggest reason Vi feels difficult is its concept of 'modes'. Unlike most editors, Vi primarily operates in two main modes: 'Normal Mode' and 'Insert Mode'.
The most critical skill is to switch between these two modes freely.
i
, a
, or o
.i
: Begin inserting text before the cursor.a
: Begin inserting text after the cursor.o
: Open a new line below the cursor and begin inserting.Esc
key. (This is the most important key!)"If you don't know what's happening, just press Esc
to return to Normal Mode." This is the first principle of mastering Vi.
In Normal Mode, pressing the colon (:
) activates the 'Command-line Mode' at the bottom of the screen, where you can input commands.
:w
: write (save the file):q
: quit (exit the editor):wq
: write and quit:q!
: force quit without saving changes (when you want to discard your edits)vi filename
: Open a new or existing file.Vi's true power is revealed when you can freely edit text using only the keyboard, without a mouse. All the following commands are used in Normal Mode.
h
, j
, k
, l
: Move the cursor left, down, up, and right, respectively. (This is efficient as it allows you to navigate while keeping your hands on the keyboard's home row.)w
: Move to the beginning of the next word.b
: Move backward to the beginning of the previous word.0
: Move to the beginning of the current line.$
: Move to the end of the current line.gg
: Go to the very first line of the file.G
: Go to the very last line of the file.x
: Delete the single character under the cursor.dw
: delete word from the cursor to the end of the word.dd
: delete the entire line the cursor is on.u
: undo the last action.Ctrl + r
: redo the undone action.yy
: yank (copy) the current line.p
: paste the copied content below the cursor.Pro-tip: After deleting a line with dd
, you can press p
to paste that line elsewhere. In Vi, 'cut' is effectively the same as 'delete and then paste'. You can also combine numbers with commands: 3dd
deletes three lines, and 3yy
yanks three lines. This combination drastically reduces repetitive tasks.
Once you're comfortable with the basics, it's time to explore advanced techniques that will elevate your productivity to the next level.
/search_term
: Search forward (down) for the 'search_term'.?search_term
: Search backward (up) for the 'search_term'.n
: Jump to the next search result.N
: Jump to the previous search result.:%s/old_word/new_word/g
: In the entire file (%
), substitute all (g
) occurrences of 'old_word' with 'new_word'. For developers, this is a magical command.One reason Vi can feel difficult is that text selection isn't immediately intuitive. This is where 'Visual Mode' comes in. It allows you to select blocks of text visually, much like in modern editors.
v
: Enter Visual Mode (character-wise selection).V
: Enter Visual Line Mode (line-wise selection).After selecting your desired area in Visual Mode, you can press d
to delete it or y
to yank (copy) it. This is a helpful feature that significantly flattens Vi's learning curve.
Vi is, without a doubt, an unforgiving and challenging tool at first. However, by mastering just the functions introduced in this guide, you will be able to confidently handle most text editing tasks you encounter in the terminal.
The philosophy of Vi is 'efficiency'. The joy of controlling everything with just the keyboard, coding without interrupting your flow of thought—that is why Vi has been cherished by countless developers for decades. Start today by trying to open even a small configuration file with Vi. Before you know it, you'll find yourself flying across the keyboard.