9. vi

vi Command in Linux

vi is a text editor in Linux, used to view and edit files directly in the terminal.

It's one of the most powerful and widely available editors on Unix/Linux systems.


🔹 Basic Syntax:

vi filename

🔹 Example:

vi notes.txt

👉 This opens (or creates, if not exists) a file named notes.txt for editing.


🔹 Modes in vi:

  1. Normal Mode (default when you open vi)
    – For navigation and commands

  2. Insert Mode
    – For typing text
    – Press
    i to enter Insert mode

  3. Command Mode
    – For saving, quitting, searching
    – Press
    : to enter command mode


🔹 Basic Commands:

Action

Key

Enter Insert Mode

i

Save File (Command Mode)

:w

Quit vi

:q

Save & Quit

:wq or ZZ

Quit without saving

:q!

Delete a line

dd

Copy a line

yy

Paste

p

Move up/down

k / j

Move left/right

h / l


🔹 Example Flow:

  1. Open a file:

vi hello.txt
  1. Press i → Enter Insert Mode

  2. Type:

Hello, this is my first vi file!
  1. Press Esc → Go back to Normal Mode

  2. Type :wq! → Save and quit

    Try with Linux Practice