vi, and a final challenge

INPUT · Slides

Modes, and how to get out

01 / 08

Every server has it

The last chapter at last. vi first.

Until now, to make a file, you have written this.

echo '#!/bin/sh' > check.shecho 'ip link ...' >> check.sh

A line at a time. Get it wrong and you start again.

want to mend line 3 -> type the whole thing once more

That is hard going. So you need an editor.

Hence vi.

> vi is on every server there is

That is the first reason for learning it.

a server you have just rented         vi is therea Linux built as small as possible    vi is therea machine half broken, being mended   vi is there

Other, handier editors have to be installed first. And a machine with no network cannot even install one. In such a place, vi is the last thing you can lean on.

There is another reason.

I want to mend one line of a settings file

That kind of work is very common in practice. Open, mend one line, save, get out. Do that and nine cases in ten are covered.

What this lesson teaches is the most important part of it — how to get out for certain. The way back, before the skill.

02 / 08

What "it will not type" really is

Everyone who opens vi for the first time says this.

I type and nothing goes in!Ctrl-C does not end it!

It is not broken. vi works on an idea called modes.

There are only two.

ModeWhat you type becomesHow to get in
command modea commandEsc
insert modelettersi and others

Just opened, you are in command mode. So typing hello is read like this.

h  move lefte  to the end of the wordl  move rightl  move righto  make a line below and enter insert mode

Run as five commands. The letters are not failing to go in; something else is being done.

Why built this way? There were the circumstances of when vi was born.

around 1976no mousesome machines had no arrow keysredrawing the screen was slow

Everything had to be done from the keyboard alone. So "when you type letters" and "when you operate" were kept apart.

It still means something today.

x      remove one characterdd     remove one line3G     jump to line 3

You operate without moving your fingers from the home row. That is why practised people are fast.

But it is confusing at first. So one thing to remember is enough.

> When in doubt, Esc

No harm however many times you press it. Press it and you are back in command mode, always.

03 / 08

Learn the way out first

The way back before the skill. Three commands for getting out.

:q    get out (when you have changed nothing):wq   save and get out:q!   get out without saving (throw it away)

How to type them.

1. press Esc (back to command mode)2. type : (a : appears at the very bottom of the screen)3. type q!4. Enter

Always start from Esc is the manner. Then you need not mind which mode you are in.

Remember when each is for.

What you typeWhen
:qyou only looked and changed nothing
:wqyou mended it and want to keep it
:q!you made a mess and want to throw it away

Type :q after changing something and you are refused like this.

No write since last change (:q! overrides)[Hit return to continue]

It means "you have not saved. To throw it away, :q!". A kindness, so that nothing is lost by accident.

When [Hit return to continue] shows at the bottom, press once first to close the message. Until it closes, no other command is taken.

That :q! becomes your charm.

> Whatever you have done, :q! gets you out with the file as it was

you typed a command you did not knowthe screen has gone to a messyou cannot tell what you touched

All of them: Esc then :q! and it is as it was. Nothing has happened to the file.

Know those two and vi is no longer frightening. Touch it in peace.

04 / 08

Opening it, and reading the screen

Opening is easy.

~ $ vi memo.txt

It is fine if the file is not there.

there      its contents come upnot there  it starts empty (made when you save)

Mind that opening alone does not make it. Get out with :q and not one file is added.

The screen looks like this.

aaabbbccc~~~- origin.txt [Modified] 1/3 33%

Know what the ~ means.

~ = there is no line here

Not an empty line. A mark for "below the end of the file". So a row of ~ only means the file is short.

The very bottom line tells you how things stand.

What showsMeaning
origin.txtthe file you have open
[Modified]changed and not saved
1/3on line 1 of 3
33%roughly where you are

If [Modified] is showing, you have not saved. Get into the habit of looking here and you will see a :q refusal coming.

In insert mode, this can show on the bottom line as well.

-- Insert --

The screen tells you which mode you are in. When stuck, look down.

05 / 08

Save only, and save and get out

Hold on to the difference between :w and :wq.

:w    save (stay inside vi):wq   save and get out

Where :w comes in.

editing for a long stretch  | :w partway (save this far)  | carry on editing  | :w again:wq at the end

Save often. The manner for not losing work in progress.

You can save under another name too.

:w backup.txt

Leave it under another name without changing the original. Rather like doing the cp of chapter 2 from inside vi.

before mending a setting, take a copy with :w as-it-was.txt

Take a copy before you touch — the manner from chapter 12 as well.

There is a short way, too.

ZZ

Capital Z twice, meaning the same as :wq. No : and no Enter needed.

:wq + Enter   4 pressesZZ            2 presses

The more used, the shorter to type. The same thinking as the alias of chapter 10.

To gather them up.

:w      save only:wq     save and get outZZ      the same (shorter):q      get out (when nothing is changed):q!     throw away and get out

These five are enough. You can work without knowing any others.

06 / 08

What happens when you type in command mode

Let us look a little further into "it will not type". Typed in command mode, letters work as commands.

Here are the ones that cause accidents.

What you typedWhat happens
xthe character under the cursor goes
dda whole line goes
oa line is added below and you enter insert mode
Jthe next line joins on
uthe last change is undone

Typed unknowingly, it looks as if things vanish by themselves. That is why people say "vi breaks your files".

But do not worry. There are two ways back.

u      undo the last change:q!    throw everything away and get out

Both are one move. So rather than being too afraid to touch it, hold :q! and touch it; you will improve faster.

Learn how to check which mode you are in, too.

1. press Esc (you are certainly in command mode)2. look at the very bottom of the screen

If -- Insert -- has gone, you are in command mode.

A word on Ctrl-C not working. As in chapter 8, Ctrl-C sends the signal called INT.

vi catches INT and ignores it

It ignores it on purpose. Losing your work mid-edit by a slip would be awkward. So to leave vi you use the :q family.

inside vi, Ctrl-C does nothing -> use :q!

Know that alone and "I cannot get back" disappears.

07 / 08

Why still vi today

A fifty-year-old tool is still in service for reasons.

1. it is everywhere2. it is light even used from far away3. it works from the keyboard alone

The second matters more than you would think.

logging into a distant server to edit  -> redrawing a lot of screen is slow  -> vi redraws only the lines it needs

Usable on a thin line. Born in hard times, and light ever since.

One more happy thing. The finger movements of vi have carried into other tools.

less: G / q / /search    the same as vi (chapter 2!)inside man               the samethe q of top             the samethe vi mode of many editors

When you learned less in chapter 2, you already knew part of vi.

G    to the endgg   to the top/    searchn    to the nextq    get out (less)

The same moves working across tools is one of the good things about the Unix world. Learn one and the next comes easier.

One honest word too.

> I will not say vi is the best editor there is

For day-to-day development, use something handier. But having one thing you can use when there is nowhere to turn matters, as a craftsman's toolbox.

This chapter goes as far as "mend one line and save". That is plenty for real use.

08 / 08

Now have a go

Here is what this lesson uses.

vi file          openi                enter insert modeEsc              back to command mode:w               save:wq              save and get outZZ               the same (shorter):q               get out (when nothing is changed):q!              throw away and get out

The form for typing is this.

Esc -> : -> the command -> Enter

Always from Esc.

Look at the bottom of the screen too.

[Modified]      not saved-- Insert --    you are in insert mode~               there is no line here

Three things to remember most today.

1. just opened, you are in command mode (letters become commands)2. when in doubt, Esc3. whatever you do, :q! gets you out with the file as it was

And today's conclusion.

> Knowing the way out, you have nothing to fear

This is not only about vi. Touching a new tool, check how to stop it first. The q of less, the q of top, Ctrl-C, and :q!. With a way back, you can try things boldly.

This lesson does no serious editing yet. You open, type a character or two, save, throw away, over and over.

Repeating until the fingers know it is the point of this lesson. The next lesson does the ways of starting to type, and the one after that, moving and editing. Let us type.