vi, and a final challenge

INPUT · Slides

Copying, and pasting

01 / 08

Do not type a similar line twice

Look at a settings file and you notice similar lines in a row.

host web1host web2host web3

All three differ by one character at the end. Typing that three times is a waste.

write one linecopy itpaste itmend one character

Grow it without typing it again. That is what this lesson does.

Only two commands are used.

yy   take the line you are on into store (copy)p    paste what is in store

yy is yank, p is put.

And there is another important use.

dd   remove a line (it goes into store as well)p    paste it somewhere else

Remove and paste, and the line has moved.

dd -> move -> p    move a line

Cut and paste, an operation you know well. vi has it properly.

Here is what this lesson gives you.

yy p     grow linesdd p     move linesx p      swap charactersP        paste above3yy      copy three lines together

Less typing is what this lesson is worth. When you notice yourself typing the same thing twice, think whether it could be copied.

02 / 08

yy copies — the file does not change

yy first. It takes the line the cursor is on into store.

one     <- you are heretwothree  | yy(one is in store)

The important thing is that nothing in the file changes.

pressed yy  |the screen is the sameno [Modified] either

Only copying, so nothing happens. You may press it in peace.

Where did the store go, you may wonder.

into the "store" inside vi

Invisible. You do not know until you paste.

A number copies several lines.

yy    one line3yy   three lines (from this line, three downward)

The same shape as dd.

dd    remove one line2dd   remove two linesyy    copy one line3yy   copy three lines

Number + command, the same everywhere.

y chooses ranges too. The same shape as d.

yw    copy a wordy$    copy to the end of the lineyy    copy the line
d + range   removec + range   changey + range   copy

Three verbs, ranges in common, as the last lesson said. So yy is easy to learn. It is dd with the removing swapped for copying.

03 / 08

p pastes — below or above

Copied, now paste.

p    paste below the cursor lineP    paste above the cursor line (capital)
onetwo     <- you are herethree

Paste one from here and you get this.

PressResult
pone two one three
Pone one two three

Below or above, and no other difference. The same relation as o and O.

o p    lower case is belowO P    upper case is above

Upper case is above, a rule that holds here too.

Remember where the cursor lands after pasting, too.

pasted with p  |the cursor moves to the pasted line

Which is handy.

yy      copyp       paste (you are on the pasted line)$ r 2   mend that line straight away

Paste and mend at once. Copy host web1, paste it, mend the end to 2 — that flow runs in one breath.

Press it again and it grows.

yy p p p

Three more of the same line. For settings for ten machines, this will do.

One caution. The store is overwritten.

yy      one into storedd      removed another line (the store is swapped)p       the removed line is pasted (not one)

There is only one store. So "copy, then paste" is safest done straight through. Slip a removal in between and the store is swapped.

04 / 08

Move lines with dd and p

Here is the interesting part. A line removed with dd goes into the same store.

dd    remove (it goes into store too)p     paste

Which means this.

dd -> move somewhere else -> p

The line has moved. Cut and paste.

Tried out, it goes like this.

one     <- you are heretwothree  | dd (one goes)two     <- the cursor is herethree  | j (to three)  | p (paste below)twothreeone     <- moved

You can change the order. Used when mending the order of lines in a settings file.

Here are the much-used combinations.

dd G p     move that line to the enddd gg P    move that line to the topdd j p     move it one down

Only the movement in the middle changes. The G and gg of an earlier lesson tell as they are.

The same works for characters.

ab^ you are here  | x (a goes and enters the store)b  | p (paste behind the cursor)ba

Two characters swapped. xp is the standard for "a slip put them the wrong way round".

porrt -> mendteh -> mend to the

Mended in two presses.

Mind the difference between pasting lines and characters here.

a store holding a line        p pastes below the linea store holding characters    p pastes behind the cursor

How it pastes follows what is in the store. vi remembers, so you need not mind. Press it and you will see.

05 / 08

Making settings for ten machines

Let me show where this lesson's skill tells most.

host web1

Say you want this for ten machines. Typed, it is this.

host web1host web2...host web10

The same thing ten times. More chances to mistype, too.

With vi it is this.

yy         copy one linep p p ...  paste (as many as you need)

The same lines in a row. Then only the numbers to mend.

2G $ r2    the end of line 2 to 23G $ r3    the end of line 3 to 3...

Mending one character at a time. You did r last lesson.

Better still, there is this way.

yy p $ r2      copy, paste, mendyy p $ r3      copy that, paste, mend

Copy the mended line. Then the cursor is on that line after pasting, so you carry straight on.

Think back to chapter 5 here. The shell can do the same.

for i in 1 2 3; do  echo "host web$i" >> settings.confdone

Making it with a loop (chapter 11).

Which to use? Like this.

regular and many              make it with fora little different, and few   copy it in vi

If there is a rule, leave it to the machine. For ten, for; for three, vi may well be faster.

What matters is that you can do both. You have them both already. You can choose by the case. That is what adding tools means.

06 / 08

There is only one store

Let me write one caution out properly.

> There is only one store

yy     line A into storedd     removed line B (the store swapped to line B)p      line B is pasted

Line A is gone. Overwritten.

So the manner is this.

copied, so paste at once

Do not slip a removal in between. If you must, paste first.

Here are the commands that fill the store.

yy yw y$    copy (the file does not change)dd dw D x   remove (they go into store too)cw cc       change (what is removed goes into store)

The removing commands rewrite the store too, which is where slips come from.

There is no mark to tell by, so you must remember. But do not worry; make pasting straight after your habit and you will not be caught.

One more thing worth knowing.

the store goes when you leave vi
vi a.txt -> yy -> :wqvi b.txt -> p        nothing to paste

You cannot carry it to another file (not in this environment). To copy across files, you do this.

sed -n '3p' a.txt >> b.txt

Use the shell tools. The sed of chapter 5.

copying inside vi     within one filecopying in the shell  across files

There is a division. You need not do everything in vi. Back at the terminal there are tools better suited. The right tool for the place is a thought this course has come back to many times.

07 / 08

Look at the line count before pasting

Pasting is prone to the mistake of growing too much.

meant to press p, and your finger slipped into p p p  |three lines more

Unlike removing, growing is hard to notice. The screen looks much the same at a glance.

So look at the usual place.

- settings.conf [Modified] 2/5 40%

The /5 part is the line count.

before   2/2after    3/3     <- one more (right)after    5/5     <- three more (too many)

You notice before you save.

Mending it when it grew too much uses last lesson's tools.

dd     remove the stray linesu      undo the last paste:q!    throw it all away and start again

Any of them saves you.

Remember how to check once you are out, too.

wc -l < settings.conf          count the linesgrep -c 'host' settings.conf   how many machinessort settings.conf | uniq -d   any identical lines

The last is from chapter 5. Identical lines come out.

host web2host web2      <- forgot to mend the number

The commonest mistake when copying and pasting is this. Forget to mend the number and you have two of the same line.

uniq -d finds it

Let the machine look. Surer than comparing ten lines by eye.

Copying and pasting is fast, but being fast, it needs checking. There are places where typing it again is surer. Three lines, copy; a hundred, make them with for; two, type them — choose by the amount.

08 / 08

What to take away from this lesson

To gather up. Copying and pasting are in place.

yy    copy one line3yy   copy three linesyw    copy a wordp     paste belowP     paste above

Put with removing, you can move things about.

dd p      move a linedd G p    a line to the endx p       swap characters

That "removing" and "pasting" use the same store is what I would most have you take from this lesson.

what dd removes is in the store

So removed, it comes back if you paste. A way back other than u.

After five lessons, the skills of vi are nearly all in place.

in and out   :wq :q! Escwrite        i a I A o Omove         h j k l 0 $ w b gg G 3Gremove       x dw D dd r cwcopy         yy yw p Pundo         urepeat       .

Settings-file work is all covered by this.

Only one thing is missing.

in a 300-line filefind the line that says "port"

Searching is beyond you. Knowing the line number you can go with 300G, but not knowing leaves you stuck.

The next lesson does that.

/port -> ⏎    find port and jumpn             to the next port:%s/80/8080/g replace them all

Search and replace. Rather like doing the grep and sed of chapter 5 from inside vi. Come that far and vi is your tool.