Read

Module 3 · Text, redirection and pipes

The vi editor, part 1: modes, moving, saving

vi is on every Unix, including this one. Learn the two modes, how to move, how to type text and, above all, how to save and get out.

What you will learn

  • Tell command mode from insert mode and switch between them with i, a, o and Esc.
  • Move with the arrow keys or h j k l, and jump with 0, $, gg and G.
  • Save and quit with :w, :q, :wq and :q!, and understand what each one does.

Sooner or later you will be on a machine with no graphical editor, no nano, and a config file that needs one line changed. vi will be there; it has been part of Unix since 1976 and POSIX requires it. The BusyBox vi in this VM is a compact version with the essential commands, which is exactly the subset worth learning first. Nothing here is hard, but one idea takes getting used to: modes.

Two modes

In command mode, which is where vi starts, every key is an instruction: j moves down, x deletes a character, : opens a prompt at the bottom for longer commands. In insert mode, keys type text, like any editor. You enter insert mode with i (insert before the cursor), a (append after it), o (open a new line below) or O (open one above). You leave it with Esc. When in doubt, press Esc; pressing it in command mode is harmless.

The typical confusion is typing text while still in command mode: letters do odd things and the file seems to change at random. The fix is always the same: Esc, then u to undo the last change, then i and type again. BusyBox vi shows - Insert - in the bottom line while you are inserting; glance at it when unsure.

Moving around

KeysMove
h j k l (or arrows)left, down, up, right
w / bnext / previous word
0 / $start / end of line
gg / Gfirst / last line of the file
:12 or 12Ggo to line 12
Ctrl-F / Ctrl-Bpage forward / back

Saving and quitting

Colon commands are typed in command mode and end with Enter. :w writes (saves) the file, :q quits, :wq does both, and ZZ (two capital letters, no colon) is a shortcut for :wq. If you have unsaved changes, :q refuses; :q! quits anyway and throws the changes away, which is the escape hatch when you have made a mess. :w newname saves a copy under another name.

~% vi notes.txt          # opens (or creates) the file, in command mode
i                        # enter insert mode
hello from vi            # type the text
<Esc>                    # back to command mode
:wq                      # write and quit
~% cat notes.txt
hello from vi
~% vi notes.txt
G                        # jump to the last line
o                        # open a line below and start inserting
second line<Esc>
:wq
~% cat notes.txt
hello from vi
second line

A few more command-mode keys you will want immediately: x deletes the character under the cursor, dd deletes the whole line, u undoes, and . repeats the last change. The next lesson covers editing properly, including search and replace; for now, practise opening a file, typing a line or two, and saving.

Commands in this lesson

CommandWhat it does
vi FILEOpen FILE (created on first save if it does not exist).
i / a / oInsert before cursor / after cursor / on a new line below.
EscReturn to command mode.
0 $ gg GStart of line, end of line, first line, last line.
:wSave.
:wq or ZZSave and quit.
:q!Quit without saving.
uUndo the last change.

Quiz

  1. In which mode does vi start?

    • Insert mode
    • Command mode
    • It depends on the file
  2. You typed some text and want to save and leave. What do you do?

    • Type :wq and Enter while still inserting
    • Press Esc, then type :wq and Enter
    • Press Ctrl-C
  3. What does `:q!` do?

    • Saves and quits
    • Quits, discarding unsaved changes
    • Deletes the file
  4. Which key opens a new line below the cursor and enters insert mode?

    • `o`
    • `a`
    • `G`
  5. How do you jump to the last line of the file?

    • ` TempMV (TempVM) — Temporary Virtual Machines in your browser · Máquinas virtuales temporales
    • `G`
    • `gg`

Practice

  1. With vi, create /root/lab/l28/hello.txt containing the single line `hello from vi`, then save and quit. The check only looks at the file, so you may use another method if you must, but try vi.

  2. /root/lab/l28/list.txt has two lines, `one` and `two`. Open it in vi, go to the last line, add a third line `three` below it, and save.

  3. You are in command mode in vi with changes made. Which command saves the file and quits?

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.