Agentic AI with Java: Live Cohort
LinuxLinux Fundamentals

Vi Editor Basics

So far, we've worked with files using commands such as cat, head, and tail. These commands are useful for viewing content, but when you need to edit a file directly from the Linux terminal, a text editor is more practical.

vi is one of the most widely available terminal-based editors on Linux systems. Learning its basic commands is useful when working with remote servers where a graphical editor may not be available.

What is Vi?

Vi stands for Visual Editor. It is a terminal-based text editor used to create and modify files.

To open a file:

vi devops.txt

If the file doesn't exist, vi can create it when you save the file.

You can work with files containing any kind of text-based content, for example:

vi notes.txt
vi config.yml
vi application.json
vi main.tf

The file extension doesn't determine whether vi can edit the file. If the content is text, vi can generally be used to edit it.


Understanding Vi Modes

The most important concept to understand in vi is that it works through different modes.

For beginners, the workflow can be understood using these three states:

  1. Command Mode
  2. Insert Mode
  3. Escape / Return to Command Mode

Technically, Esc is a key, not a separate editing mode. Pressing Esc takes you out of Insert Mode and returns you to Command Mode. Commands such as :wq and :q! are then entered through Command-Line Mode.


Command Mode

When you open a file using vi, it starts in Command Mode.

In Command Mode, keyboard keys are interpreted as editor commands rather than normal text.

You can use Command Mode to:

  • Navigate through the file
  • Delete lines
  • Copy lines
  • Paste lines
  • Move the cursor
  • Perform other editing operations

You do not normally type regular text directly in Command Mode.

For example:

vi devops.txt

When the file opens, you are initially in Command Mode.


Insert Mode

Insert Mode is used to enter and modify text inside the file.

From Command Mode, press:

i

You can now type normally.

For example:

Linux is widely used for servers and cloud infrastructure.

Depending on the vi implementation, you may see:

-- INSERT --

at the bottom of the terminal.

Entering Insert Mode

Command Mode
      ↓
      i
      ↓
Insert Mode

You can now add or modify the file content.


Escape: Returning to Command Mode

After entering or modifying content, press:

Esc

This leaves Insert Mode and returns to Command Mode.

Insert Mode
     ↓
    Esc
     ↓
Command Mode

This is an important step because commands such as dd, yy, p, and :wq are not entered as normal text.


Saving and Exiting Vi

After editing the file:

  1. Press Esc
  2. Type :wq
  3. Press Enter
Esc
:wq
Enter

Here:

  • w → Write (save)
  • q → Quit

Therefore:

:wq

means save the changes and exit vi.


Exit Without Saving

If you have made changes but do not want to save them:

  1. Press Esc
  2. Type:
:q!
  1. Press Enter

Here:

  • q → Quit
  • ! → Force the operation

So:

:q!

exits vi without saving the unsaved changes.

Use :q! when you want to discard the changes made since the last save.


Basic Editing Commands

Once you understand Command Mode and Insert Mode, a few basic commands are enough to perform common editing tasks.

Command / KeyPurpose
iEnter Insert Mode at the cursor
EscReturn to Command Mode
ddDelete the current line
yyYank (copy) the current line
pPaste after the current line

Delete a Line

Place the cursor on the line you want to remove and press:

dd

The entire line is deleted.

For example:

Line 1
Line 2
Line 3

Place the cursor on Line 2 and press:

dd

The result becomes:

Line 1
Line 3

Copy a Line

To copy a line, place the cursor on that line and press:

yy

yy means yank the current line.

The copied line is stored in vi's internal buffer.

For example:

Line 1
Line 2
Line 3

Place the cursor on Line 2:

yy

The line is now available to paste.

Paste a Line

After copying a line using:

yy

press:

p

to paste it after the current line.

For example:

Line 1
Line 2
Line 3

After copying Line 2 and pressing p:

Line 1
Line 2
Line 2
Line 3

Quick Reference

  • Vi is a terminal-based text editor commonly available on Linux systems.
  • Use vi <filename> to open or create a file.
  • vi initially opens in Command Mode.
  • Press i to enter Insert Mode and type or modify content.
  • Press Esc to return to Command Mode.
  • Use :wq to save and exit.
  • Use :q! to exit without saving.
  • Use dd to delete a line.
  • Use yy to copy a line.
  • Use p to paste a copied line.
  • Understanding the current mode is the most important part of using vi.

Written By: Muskan Garg

How is this guide?

Last updated on