vi, and a final challenge

INPUT · Slides

Removing, and mending

01 / 08

To mend is to remove and write

Last lesson you learned to get to the place you meant. Arrived, what do you do next?

you went to line 50  |you want to mend it

"Mending" is usually a pair of these two.

1. remove2. write

The writing you did last lesson (i a I A o O). So this time it is removing.

What you remove comes in sizes too.

one character         xone word              dwmid-line to the end   Done line              dd

Chosen by size, like movement.

And the amount can be named with a number.

3x     remove three characters2dw    remove two words2dd    remove two lines

The same shape as last lesson's 4j. "Number + command" is the same everywhere.

There are also ones that remove and write in a single breath.

r    replace one character onlycw   replace a word

Remove and then type, in one move. Very handy.

And the one not to forget is this.

u    undo the last change

Remove too much and it comes back. So you may remove boldly. This lesson is practice at removing, so let us go with u as a charm.

02 / 08

Remove one character with x

The smallest way to remove. The character under the cursor goes.

aaa bbb ccc^ you are here  | xaa bbb ccc

Most used for mending a typing slip.

prot 80    a slip

To mend it, this.

move to character 2 (l)x to remove the rthen behind character 1...

A little bothersome. So a way to mend by whole words comes later.

The good thing about x is that it does not enter insert mode.

press x -> it goes -> still command mode

No Esc needed. So press x again and again and things keep going.

x x x    three characters go3x       the same (2 presses)

A number is faster.

Know what it does when there are not enough characters, too.

ab^ type 5x here  |(only what is there goes -> an empty line)

It is not an error. It removes what is there and stops. A gentle arrangement.

A word on the difference from the Delete key.

Delete   the character at the cursor (the key is far)x        the character at the cursor (your hand stays put)

The same thing with your hand left where it is. That is the thinking of vi. The same reason as last lesson's h j k l.

03 / 08

Remove by words with dw

A character at a time is sometimes slow.

port 80     ^ you want to remove the 80

Two x would do, but a value of 8080 is four and localhost is nine. So there is a command that removes by words.

dw   remove from the cursor to the head of the next word
aaa bbb ccc^  | dwbbb ccc

Did you notice the whitespace goes too? Not only aaa but the space behind it.

dw = delete word

Properly, it removes "up to where the next word begins". Which is why the whitespace comes with it.

This is a kindness.

aaa bbb ccc  | dw (the whitespace goes too)bbb ccc          <- it joins up properly

Leave the whitespace and you get bbb ccc, with a stray space at the front.

Numbers work.

2dw   remove two words3dw   remove three words

The way d and w combine means something too.

d     remove (the verb)w     as far as the next word (the range)

Verb + range. So the w can be swapped for another movement.

dw    remove up to the wordd$    remove to the end of the lined0    remove to the head of the linedG    remove to the last line

Learn the movements and you have learned the ranges of removal. The 0, $ and G of last lesson serve as they are. That is the neatness of how vi is built.

04 / 08

dd for a line, D to the end of the line

A whole line is dd. You used it last lesson too.

dd    remove the line you are on, whole2dd   remove two lines
onetwo     <- you are herethree  | ddonethree

The line below moves up. One line fewer, that is.

The shape of typing d twice is the same thinking as gg.

dw    the range is "a word"dd    the range is "a line"

The second d stands for "line".

There is also D (capital).

D    remove from the cursor to the end of the line
aaa bbb ccc    ^ you are here  | Daaa 

The line stays and the rest goes. With dd the line goes; with D the line merely empties.

dd   the line is gone (the count drops)D    the line stays (the contents shrink)

The difference matters. In a settings file, when you want to keep the line but rewrite the value, it is D.

D means the same as d$.

d$   remove to the end of the lineD    the same (shorter)

The rule that what is much used has a short form. Like ZZ being the short form of :wq.

One caution. D leaves the whitespace.

aaa bbb    ^ D here  |aaa      <- one space is left behind

Invisible, of course. What you cannot see, count with wc -c. The counting of chapter 3 tells at times like these.

05 / 08

r and cw — remove and write in one move

There are ones that do "remove and write" in one move. One character first.

r    replace one character only
port 80      ^ press r here and type 1  |port 81

Four moves — x, i, type, Esc — become two.

The good thing about r is that it does not enter insert mode.

r -> type one character -> still command mode

No Esc needed. For mending one character, this is the fastest.

r = replace

There is one for replacing a whole word, too.

cw   remove the word and enter insert mode
port 80     ^ press cw here  | the 80 goes and you are in insert modeport   | type 8080 and Escport 8080

Removing and typing run on together. dw, then i, then type, gathered into one.

c = change

Hold on to the difference from d.

dw   remove only (still command mode)cw   remove and type (into insert mode)

c enters insert mode. So do not forget the Esc. The accident of last lesson happens here too.

c chooses ranges as well.

cw   change a wordc$   change to the end of the linecc   change a whole line

The same shape as d. Only the verb differs.

d + range   removec + range   change

Learn one and you use two. Which is why vi is said to have little to learn.

06 / 08

Undo with u, repeat with .

To practise removing, take this in hand first.

u    undo the last change
removed it with dd  | uit came back

Back in one move. u is undo.

But the u of this environment has a limit.

dd dd u    -> only one line comes back

Only the last single move. It cannot go two moves back.

noticed a mistake, so u at oncemoved on several steps, so :q!

A choice between them. When you no longer know what you did, getting out without saving and starting again is faster.

And there is a command for repeating.

.    do the last change once more
typed dd  | .another line goes  | .another line again

Repeating the same thing. All you press is one . (a period).

u and . work in just opposite ways.

u    one back.    once more

You can go to and fro. Which suits mending while you try things.

type dwremoved too much -> u to bring it backlooks good -> . to do the same to the next

"Try one place, and repeat if it is good" becomes possible. A different way from the sed of chapter 5.

sed    all of it at once (fast)vi     one place at a time, watching (checkable)

Choose by whether it can be taken back. For an important file, doing it while watching is safer.

07 / 08

Take a copy before you remove

Now that you know the removing commands, here is an important manner.

> Take a copy before you touch

cp settings.conf settings.conf.bakvi settings.conf

Type the cp of chapter 2 first. One extra move, and an accident stops being an accident.

You can do it from inside vi too.

vi settings.conf:w settings.conf.bak    take a copy under another name(mend from here):wq

The :w name of an earlier lesson. Take a copy the moment you open it is another way.

Why make so much of it? Because the removing commands fail in ways that are hard to notice.

2dd   meant two lines, but typed 20dddG    everything to the last line went

A typing slip tells heavily. u goes back only one move, so notice late and you cannot go back.

Keep a way of noticing, too.

- settings.conf [Modified] 3/3 100%

Look at the line count at the very bottom.

before   3/10after    3/3     <- seven lines fewer

Fewer than you thought means something is wrong. Notice here and :q! saves you.

To gather it up, the steps for removing are these.

1. take a copy2. remove3. look at the line count at the bottom4. :wq if it is good, :q! if it is odd

Check before you save. Until you type :wq, the file is untouched. This order — check, then save — is what guards you.

08 / 08

What to take away from this lesson

To gather up. Removing can now be chosen by size.

x     one characterdw    one wordD     to the end of the linedd    one line

Replacing is two.

r     one character only (no insert mode)cw    a word (into insert mode)

And numbers and repeating.

3x 2dw 2dd    add a number.             once moreu             one back

Learning the shape "verb + range" is the best of this lesson's harvest.

d + w    remove a wordd + $    remove to the end of the linec + w    change a word

What you learned as movement serves as the range. Which is why last lesson was worth doing.

Here is what vi can do so far.

open      vi filemove      h j k l 0 $ w b gg G 3Gwrite     i a I A o Oremove    x dw D dd r cwundo      usave      :w :wq ZZget out   :q :q!

Usable in practice already. Touching up a settings file is all covered.

The next lesson is "copy and paste".

yy    copy the line you are onp     paste (below)P     paste (above)

Making similar lines gets fast. Settings files often have similar lines in a row. Write one, copy it and mend it becomes possible.