vi, and a final challenge

INPUT · Slides

Moving where you meant

01 / 08

"I want to mend line 50 only"

So far you can open, write and save. But one thing is still beyond you.

I want to mend line 50 of a settings file only

As you are, you cannot move off line 1. Open vi and the cursor is always on line 1.

And pressing o fifty times will hardly do.

So you learn to move. What this lesson gives you is this.

move between lines     h j k ljump within a line     0 $ w bjump within the file   gg G 3G

Movement at three sizes.

Why not the arrow keys, you may think. Some vi allow them. But some do not.

an old machinethe connection is poorthe arrows arrive as other characters

At times like these the arrow keys do nothing. h j k l always work. So it is these you learn.

And once used to them, they are faster.

arrow keys   move your hand down to the righth j k l      hand stays put

Your fingers never leave the home row. No moving your hand for every character.

At the end of this lesson, vi finally becomes "a tool you can use". Without reaching the place you meant, there is no editing.

02 / 08

h j k l — hand stays put

The basic four. Press them in command mode.

      k (up)h (left)  l (right)      j (down)

Look at the keyboard. h j k l lie in a row where the right hand rests.

h j k l^ left end is left, right end is right

Remember them like this.

KeyDirectionHow to remember
hleftthe left end of the four
lrightthe right end of the four
jdownthe letter j reaches downward
kupthe letter k reaches upward

j and k are easy to mix up, so mind those. j has a tail going down is a good way to hold it.

Pressed, it moves like this.

onetwo     <- you are herethree  | press jonetwothree   <- one line down

The important thing is that moving changes nothing.

h j k l   only moves (the file is untouched)

You may press them about in peace. No [Modified] appears. So at first, going round and round the screen with these is a good way to get used to it.

Mind that they do not work in insert mode. A j in insert mode is the letter j. To move, Esc.

03 / 08

Jumping within a line — 0 $ w b

A character at a time is sometimes too far. Eighty l presses to the end of an eighty-character line will not do.

So there are commands that jump.

0   to the head of the line (the digit zero)$   to the end of the line

As a picture.

aaa bbb ccc^         ^0         $

One move whatever the length of the line. The I and A of last lesson ride on these two.

I = 0 then iA = $ then a

The same thinking coming out in another shape.

There are ones that jump by words, too.

w   to the head of the next wordb   to the head of the previous word
aaa bbb ccc^   ^   ^    w   w      (forward in turn)    b   b      (back in turn)

w is word, b is back.

Words are divided by whitespace and symbols. So in a settings file it tells like this.

port 80^    ^w to the head of the value

Handy when you want to mend the value only. Faster than pressing l five times.

To gather them up.

PressWhere it goes
0the head of the line
$the end of the line
wthe head of the next word
bthe head of the previous word

h l for near, 0 $ w b for far.

04 / 08

Jumping within the file — gg G 3G

Between lines, fifty presses of j is hard work too. So there are commands that jump.

gg   to line 1G    to the last line

G is capital (hold Shift). gg is lower-case g twice.

one     <- gg to heretwothree...ten     <- G to here

One move to either end of the file.

And you can jump by naming a line number.

3G   to line 350G  to line 50

A number in front. This is the shape you will use most this lesson.

The same can be done with :.

:3 -> ⏎    to line 3

The : you used for :wq last lesson. Type only a number and you jump to that line.

3G     3 presses (fast):3 ⏎   3 presses (easy to type)

Either is fine. Use whichever you like.

Now "I want to mend line 50 only" is solved.

vi settings.conf50G       jump to line 50A         type from the end of the line...       mend itEsc :wq   save and get out

When an error message says "line 50 is odd", you go straight to line 50. Errors in settings files and scripts almost always tell you a line number. Which makes 3G the movement that comes up most in practice.

05 / 08

A number in front repeats it

Did you notice with 3G? A number in front repeats it is a rule across the whole of vi.

4j    four lines down3l    three characters right2w    forward by two words5k    five lines up

No pressing the same key over and over.

Count the presses.

j j j j    4 presses4j         2 presses

The further you go, the wider the gap.

j twenty times   20 presses20j              3 presses

The rule is not for movement alone. It tells for the editing of the next lesson too.

3x    remove three characters2dd   remove two lines

Learn the shape "number + command" and it is all the same. That is how vi is built.

Notice that it reads like language, too.

4j    "four down"3x    "remove three characters"2dd   "remove two lines"

Number then verb. Close to the way English says it. So once used to it, a sentence forms in your head before you type.

Of course you can work without the numbers.

first, move correctly without numbersonce used to it, add numbers for speed

No hurry. Pressing j five times and typing 5j land in the same place.

06 / 08

Reading your position at the bottom

Which line you are on shows at the very bottom of the screen.

- nums.txt 3/10 30%

How to read it.

What showsMeaning
nums.txtthe file you have open
3/10on line 3 of 10
30%roughly where you are

3/10 is where you are. Move, and watch it change.

This helps while you are learning to move.

pressed 4j  |3/10 changed to 7/10

You confirm that you really went four lines down. No counting lines by eye.

[Modified] shows in the same place.

- nums.txt [Modified] 3/10 30%

Merely moving does not raise [Modified]. If it shows, you have typed a character somewhere.

[Modified] appeared  |press u to undo (or :q! to throw it away)

Noticed, and put back at once. Which is why the habit of looking at the bottom line is worth having.

A phone screen is narrow, so this one line is packed with information. When in trouble, look down first. It is a habit shared by everyone who uses vi.

07 / 08

Move -> mend -> Esc

Put together, what you have makes this form.

1. move      3G or w to the place you meant2. mend      type with i a I A o O3. Esc       back to command mode

Repeating those three moves is how vi is used.

Let us write out a real job. Commenting out line 3 of a settings file.

vi settings.conf3G           to line 3I            type from the head of the line# and space  typeEsc          back:wq          save and get out

Done in seven moves. Once used to it, under ten seconds.

Another, adding a note at the end of line 2.

2G          to line 2A           type from the end # main     typeEsc :wq

Can you see moving and typing put together? Neither alone makes a job.

moving only   you look but cannot mendtyping only   you can mend line 1 onlyboth          you can mend anywhere

So take last lesson and this one as one skill in a set.

The ways out when in doubt do not change.

Esc        to command modeu          undo the last:q!        throw it all away

Those three let you move boldly. Move the wrong way and Esc always brings you back to command mode.

08 / 08

What to take away from this lesson

To gather up. Movement now comes at three sizes.

a character at a time   h j k ljump within a line      0 $ w bjump between lines      gg G 3G :3repeat with a number    4j 3l 2w

The further off, the bigger the unit is the secret of speed.

Here is a guide for choosing.

the next character   h lthe ends of a line   0 $a few words          w ba few lines down     4ja distant line       3G or :3the ends of the file gg G

Choose by distance. No need for 3G when it is near, and no need to hammer j when it is far.

And the most important thing once more.

> Moving does not change the file

No [Modified] appears, so however much you press, nothing breaks. Which is why starting practice with movement is safe.

The next lesson is editing.

x     remove one characterdw    remove a worddd    remove a lineyy p  copy and pasteu     undo

Removing, copying, undoing. Put with movement, vi becomes a really fast tool.

3G dd     remove line 3$ x       remove the last character of a lineyy p      make another of the line you are on

Move, then remove. The movement of this lesson tells directly. Look forward to it.