Linucate
~ Linucate_

103.8 Basic file editing

All Levels

Introduction

This lesson is about editing text with vi.

The main question is:

How do you open, move, insert, delete, copy, search, save, and quit in vi without panic?

What you should be able to do after this lesson:

  • Understand vi modes.
  • Move through a file.
  • Insert and edit text.
  • Delete and copy text.
  • Save and quit.
  • Recognize nano, vim, and emacs.
  • Understand the EDITOR variable.

Before you start:

  • on many systems, typing vi really opens vim
  • if you feel lost, press Esc a few times to get back to normal mode
  • if you just want to leave safely, :q! is the emergency exit

Big Idea: vi Is a Modal Editor

This is the most important beginner fact.

vi does not treat all keys the same way all the time. The meaning of a key depends on the current mode.

Simple picture:

  • normal mode = move around and give commands
  • insert mode = type text

If vi feels strange, it is usually because you forgot which mode you are in.

First Safe Session

If you remember only one workflow at first, remember this:

  1. open the file
  2. press i
  3. type text
  4. press Esc
  5. type :wq

That is enough for a beginner to survive first contact with vi.

Open a File

vi notes.txt

If the file exists, it opens. If it does not exist, vi lets you create it when you save.

Move Around

Classic movement keys:

h j k l

Meaning:

  • h = left
  • j = down
  • k = up
  • l = right

Useful extra movement keys:

0
$
gg
G

Meaning:

  • 0 = start of current line
  • $ = end of current line
  • gg = top of file
  • G = bottom of file

Arrows may work on many systems, but h j k l are the classic vi skill LPIC expects you to recognize.

Search Inside a File

Search commands:

/pattern
?pattern
n

Meaning:

  • /pattern = search forward
  • ?pattern = search backward
  • n = repeat the search in the same direction

Enter Insert Mode

Common keys:

i
a
o

Meaning:

  • i = insert before cursor
  • a = append after cursor
  • o = open a new line below

To leave insert mode:

Esc

That key matters so much that many beginners press it more than once. That is fine.

Delete, Copy, Paste, Undo

Common commands:

x
d
dd
y
yy
p
u

Meaning:

  • x = delete one character
  • d = delete with a motion
  • dd = delete current line
  • y = yank with a motion
  • yy = copy current line
  • p = paste
  • u = undo last change

For a beginner, dd, yy, p, and u give you a lot of power very quickly.

Save and Quit

Important commands:

:w
:q
:wq
:q!
ZZ

Simple meanings:

  • :w = save
  • :q = quit
  • :wq = save and quit
  • :q! = quit without saving
  • ZZ = save and quit

If You Panic in vi

This is the recovery block many beginners need most.

  1. Press Esc.
  2. Decide whether you want to save.
  3. If yes, type: :wq
  4. If no, type: :q!

That is enough to recover from most beginner confusion.

Awareness of Other Editors

LPIC expects awareness of:

  • vim
  • nano
  • emacs

vim is an extended vi. nano is usually easier for beginners. emacs is another major editor family.

EDITOR

Many programs look at the EDITOR environment variable to decide which editor to launch.

Example:

export EDITOR=vi

Example output from echo "$EDITOR" after export:

vi

What this shows:

  • the shell variable is now set
  • programs that respect EDITOR may use vi by default

Common Beginner Mistakes

  • trying to type text while still in normal mode
  • trying to run commands while still in insert mode
  • forgetting to press Esc before :wq or :q!
  • deleting text and not knowing that u can undo it
  • thinking vi is random when the real issue is mode confusion

Practice Step by Step

1) First safe edit
  1. Open: vi notes.txt
  2. Press i.
  3. Type some text.
  4. Press Esc.
  5. Save and quit with: :wq
2) Delete, undo, and leave without saving
  1. Open a file in vi.
  2. In normal mode, press dd to delete a line.
  3. Press u to undo it.
  4. If you want to leave without saving: :q!
3) Search for text
  1. Open a file in vi.
  2. Type: /pattern
  3. Press Enter.
  4. Press n to jump to the next match.

Cheat Sheet

  • normal mode = move and command mode
  • insert mode = typing mode
  • h j k l = basic movement
  • 0 and $ = start and end of line
  • gg and G = top and bottom of file
  • i, a, o = enter insert mode in different ways
  • Esc = leave insert mode
  • dd = delete line
  • yy = copy line
  • p = paste
  • u = undo
  • /pattern and ?pattern = search
  • :w = save
  • :wq = save and quit
  • :q! = quit without saving
  • EDITOR = preferred editor variable
🎯

Test Your Knowledge

Complete the quiz to assess your understanding of this course's concepts.