vi, and a final challenge

INPUT · Slides

Starting to type

01 / 08

i alone would do, but

You learned i last lesson. That is enough to start typing.

i   start typing where you are

But with only that, some situations go wrong.

hello

Say you want to add there after this line. Start with i and you get this.

press i and type there  |therehello

It went in front. i is insert, and it begins in front of the cursor.

So how do you add behind? There are other commands that choose where you begin typing.

i   in front of the cursora   after the cursorI   at the head of the lineA   at the end of the lineo   making a line belowO   making a line above

Six. With these you choose "where do I begin typing".

It may feel like a lot, but remembering them is easy.

lower case   where you areupper case   at the ends of the line

Let us take all six this lesson. Mending one character gets much faster.

02 / 08

i and a — in front and behind

The two lower-case ones first. They differ in which side of the cursor you begin.

abc^ the cursor is here (on the a)

From here, typing Z with each gives this.

PressResult
i then ZZabc
a then ZaZbc

Off by one character. That is the whole difference.

Remember them by their names.

i   insert   push in (in front)a   append   add on (behind)

Which to use is decided like this.

add in front of the character you are on   -> iadd behind the character you are on        -> a

Mind that you need a to add at the end of a line. With the cursor on the last character, i can only get in front of it.

abc  ^ on the ci then Z   abZca then Z   abcZ

Only a can add at the end. So a is used more than you would think.

Both enter insert mode, so Esc when you have typed. Same as last lesson.

03 / 08

I and A — to the ends of the line

In upper case they jump to the ends of the line before you begin typing.

port 80

Wherever the cursor is, this happens.

PressWhere you begin typing
Ithe head of the line
Athe end of the line

Not having to move the cursor is what they are worth. Wherever you are in the line, one move takes you to an end.

This is very much used in practice.

port 80  | press I and type # and a space# port 80

The standard way of turning a setting off. Put a # at the head of a line and the line stops being read (chapter 11).

A is used like this.

port 80  | press A and type  # temporaryport 80 # temporary

Adding a note at the end of the line.

Remember them like this.

I, the capital of i   do i at the head of the lineA, the capital of a   do a at the end of the line

Take capitals as "the ends-of-line version". Knowing the lower-case ones, you will not be at a loss.

04 / 08

o and O — adding lines

The last two add lines themselves.

onetwo      <- you are herethree

Pressed from here, this happens.

PressWhat happens
oan empty line below and into insert mode
Oan empty line above and into insert mode

Just after pressing o.

onetwo        <- the new empty line. type herethree

Making the line and starting to type, in one move. That is what o is good for.

Doing it with i you would have to go like this.

go to the end of the linepress ⏎ for a newlinetype

Three moves become one. So whenever you want a new line, use o.

The name is open — open a gap between lines to make room.

Look at where capital O comes in, too.

port 80  | press O and type # setting# settingport 80

When you want to add above line 1, only O will do. With o it lands below. You need it for a heading or a comment at the top of a file.

05 / 08

Choosing among the six

Here they are in a list. Remember this table and you have enough.

PressWhere you begin typingDoes a line appear?
iin front of the cursorno
aafter the cursorno
Ithe head of the lineno
Athe end of the lineno
oa new line made belowyes
Oa new line made aboveyes

The top four mend, the bottom two add.

Chosen in a real situation, it goes like this.

mend a typing slip        i or aput a # at the head       Iwrite on at the end       Aadd one line of setting   oadd at the top of a file  O

Working back from what you want to do is fastest.

Whichever you press, you are in insert mode once in, all the same.

any of the six   ->  insert modeEsc              ->  command mode

While I or -- Insert -- shows at the very bottom you are in insert mode, so when in doubt, look down.

Even if you forget them all, i and Esc alone let you work. These six are for speed. Take them into your fingers little by little.

06 / 08

What you can do inside insert mode

Once in insert mode, it is ordinary typing.

type letters   they go inpress ⏎        a newline goes inBackspace      what you typed goes

That becomes a newline matters. You can write any number of lines in one go.

press itype #!/bin/shtype echo hiEsc

A two-line file. The same as typing echo twice in chapter 11.

echo '#!/bin/sh' > hi.shecho 'echo hi' >> hi.sh

You can write what you want to write, as it is. And the quoting worry disappears.

Look at the difference from command mode here.

KeyCommand modeInsert mode
move to the line belowput in a newline
xone character goesan x goes in
Escnothing happensback to command mode

The same key means something else. That is what the idea of modes really is.

One more thing to know.

> Do not use the arrow keys in insert mode

Some vi allow it, but some do not. When you want to move, press Esc first and then move — safer to remember it that way. Moving comes next lesson.

07 / 08

What goes in when you forget Esc

Let us look at the commonest accident first. Typing :wq without pressing Esc.

pressed i and finished typing(did not press Esc)type :wq

What happens is this.

what you typed:wq

The :wq goes in as letters. In insert mode, of course. It does not work as a command, so nothing is saved either.

How to notice.

no : appears at the very bottom of the screenwhat you typed shows in the body

If no : is on the bottom line, it is not a command.

Mending it is easy.

press Escpress u (the letters that went in come back out)

u is undo. You did it last lesson.

There is a surer way to mend it, too.

Esc -> :q! -> ⏎

Throw it away and start again. When you cannot tell what went in, this is faster.

The prevention is only this.

> When you have typed, Esc first

Make it a rhythm: type, Esc, type, Esc. Esc does no harm however often you press it, so press it liberally. Practised people all press it without thinking.

08 / 08

What to take away from this lesson

To gather up. There are six ways of starting to type.

i  in front     a  behindI  line head    A  line endo  a line below O  a line above

Lower case where you are, upper case at the ends of the line was the way to remember it.

Put with last lesson, you can already do this much.

vi file       openi a I A o O   start typingEsc           to command mode:w :wq ZZ     save:q :q!        get outu             undo

Open, write where you like, save and get out. You have come this far.

The common jobs of practice are within reach as well.

add a line of setting   oturn a setting off      I and a #add a note              Awrite a script          i and ⏎

Those four carry a good deal of server work.

Only one thing is missing.

I want to mend line 50 only

As things are, you cannot get there. You have no way of moving the cursor yet.

The next lesson does that. h j k l a character at a time, w by words, G to a line number — you get to the place you meant in one breath. Once there, vi is your tool.