The Vi Text Editor

Updated: Sep 21,2026

When I first started in computers, I hated the vi Editor. It was strange; it was foreign; and it was frustrating. At the time, it was pretty much the only option for editing files. Especially, when I would need to switched from system to system. My preferred text editor was seldom on those system. So, I was begrudgingly forced to learn it.

After some brief friendship building, vi has quickly become my favorite editor. Once you remember the Command vs Edit Modes; Once you realize not taking your hands of the keyboard is faster and more efficient; You're sure to make it your favorite editor as well. And you can rest assurd no matter what UNIX-Like system you encounter, it will be there.

First things, First

vi has two modes. Command Mode and Edit Mode

When you first launch vi you are in command mode

 
vi filename
 

When you are in command mode, you can't start editing the file right away.

The first two commands you need to learn are how to enter/return to command mode and how to quit

  • Esc - Puts you in or returns you to command mode. We say "Escape".
  • :q! - Quits the file without saving. We say "colon quit bang"

It's safe and normal to hit the Escape Key Esc multiple times just to make sure you are in command mode.

Navigation / Home Row

If you never learned to type, the home row is where you are supposed to keep your fingers while typing. Those keys are asdf on your left hand and jkl; on your right. Most keyboards will be marked for your index finger. Go ahead stroke your index fingers across the f and j keys there should be a raised dimple that lets you know, this is where your fingers belong.

While most of the time you can use the arrow keys to navigate, occassionally those keys may not be mappped so it helps to learn the home row keys

  • h or ← - Move Cursor LEFT
  • j or ↓ - Move Cursor DOWN.
  • k or ↑ - Move Cursor UP
  • l or → - Move Cursor RIGHT

I tend to mix up UP and DOWN when I use Home Row Navigation. It helps to remember if your fingers are correctly placed on the Home Row. Your Index finger is on j and your middle finger is on k. UP YOURS!

Editing the File

After knowing how to navigate through the file the only other commands you need to know are how to delete a character and how to begin typing

  • x - Deletes the character under your cursor
  • i - Insert. Switches you to edit mode to allow you to begin typing. When you are done typing, hit Esc to return to Command mode

Saving the File

Lastly, after making changes to the file and while in command mode

  • :w - Write the file. We say "Colon Write"
  • :q - Quit the file. We say "Colon Quit". If you made changes to the file and haven't written those changes, vi will refuse to quit.
  • :q! - Again, Quit without saving. You'll probably do this a lot.
  • :wq - Write and quit the file. We say "Colon Write Quit"

More Commands that will get you to love Vi

Navigation Shortcuts

  • G - Go to the end of the file
  • :1 - Goes to line number 1. This can be any line number in the file
  • :set number - Shows the line numbers in the file
  • :set nonumber - Hides the line numbers
  • ^ - Go to the Begining of the line. This the same character used to mean beginning in Regular Expressions
  • $ - Go to the end of the line. This is the same character used to mean end in Regular Expressions

Searching for things

  • / <regex> - Search down from cursor location. (ex: /foo)
  • ? <regex> - Search up from cursor location. (ex: ?bar)

More editing commands

  • i - Insert before cursor. Allows you to start typing, putting you in edit mode
  • I - Insert at the beginning of line. Allows you to start typing, putting you in edit mode
  • a - Append after cursor. Allows you to start typing, putting you in edit mode
  • x - Deletes the character under your cursor
  • r - Replaces the one character under your cursor
  • R - Replaces the characters under your cursor until you return to command mode. Esc

Quick edits

  • dd - Delete the entireline
  • D - Deletes everything to the end of current line
  • dG - Deletes all lines from cursor to end of file
  • dw - Delete word
  • cw - Change word
  • yy - Copies or "yanks" line
  • yw - Copies or "yanks" word
  • p - Pastes or "puts" previous delete or yank

When we "speak" vi, we don't talk about copy and paste. Instead we'll often say things like "Yank, Yank, Put" for yyp

Repeating commands

  • . - Repeats the last command
  • <number>x - Deletes N number of characters. (ex: 7x deletes 7 characters)
  • d<number>d - Deletes N number of lines. (ex: d2d deletes 2 lines)
  • d<number>w - Deletes N number of words. (ex: d3w deletes 3 words)
  • c<number>w - Changes N number of words. (ex: c2w changes 2 words)
  • y<number>y - Yanks N number of lines. (ex: y10y yanks 10 lines)
  • y<number>w - Yanks N number of words. (ex: y3w yanks 3 words)

Search and Replace

Vi can leverage complex matching rules using the special meaning of characters we call Regular Expressions sometimes called RegEx for short

You can learn the basics of Regular Expressions HERE

The Search and Replace Command is structured like: :[range]s[delimiter]{search}[delimiter]{replace}[delimiter][flags]

Examples:

  • :s,foo,bar,g - Search current line for foo and replace it with bar using the global flag
  • :10,25s/foo/bar/gi - Search lines 10 through 25 for foo and replace it with bar with the global and ignore case flags. Will match foo, Foo, FOO, etc and replace with bar
  • :%s:foo:bar:gc - Search entire file for foo and replace it with bar using the global and confirm flags. Making you answer "Yes", "No", "Always" or "Quit" to each match

Notice I used a different delimiter in each of the examples above. ,, / & : respectively. This is a handy trick when what you are searching for contains what you might normally use as your delimiter.

Other Common Commands

  • u - Undo Change
  • Ctrlr - Redo Change
  • J - Joins line with the Next line
  • o - Adds or "Opens" a new line in Edit mode
  • ~ - Change case of character. Will make a lowercase upper and an uppercase lower under cursor

Fair Warning

Once you start using vi and you learn to love its power, you may not want to use anything else.

You just might end up adding :wq to the end of all your files or emails out of habit when not using vi.

 
:wq