vi, and a final challenge

INPUT · Slides

Searching, and replacing

01 / 08

When you do not know the line number

By now vi is fairly usable. But one situation still leaves you stuck.

in a 300-line settings filemend the line that says "port"

You do not know which line. 3G is no use without the number.

You could press j and look, but 300 lines will not do.

So you search by contents.

/port -> ⏎

You jump to the line that says port. That is the star of this lesson.

Rather like grep, you may think. Just so.

grep port settings.conf   which line has it (chapter 5)/port inside vi           jump to that line

The business of searching is the same; grep shows you, and vi takes you there.

There is one more thing this lesson does.

:%s/80/8080/g

Replace them all. Rather like doing sed from inside vi.

sed 's/80/8080/g' file   makes output (chapter 5)the :%s/80/8080/g of vi  mends the file you have open

Here is what this lesson gives you.

/word n   search down, to the next?word     search up:s        replace in this line:%s       replace in every line

Able to search, and vi is complete. Only the test of skill is left.

02 / 08

Search and jump with /

How to type it.

1. Esc (make it command mode)2. type / (a / appears at the bottom of the screen)3. type the word to look for4. ⏎

Like :, a place to type appears at the very bottom of the screen.

/port

Press and the cursor flies to that word.

There is one property to remember.

> / searches from below the line you are on

1: port 80     <- you are here2: host web13: port 80

Type /port from here and you jump to line 3. Line 1 is passed over.

it looks beyond where you are

This is a kindness. Stopping where you already are would let you find nothing further.

Know what happens when it is not found, too.

/nothing -> ⏎  |Pattern not found

"Not found" comes up. And the cursor does not move.

found       you jumpnot found   you do not move (a message appears)

This matters. Press dd thinking you moved and the line you are on goes.

/nothing -> ⏎ (you have not moved)dd            <- this line goes!

So after searching, look at the screen and confirm that you jumped. See whether the line number at the very bottom has changed.

- settings.conf 1/5 20%  | /log -> ⏎- settings.conf 4/5 80%     <- jumped

The number changing means you jumped. Slipping this check in prevents accidents.

03 / 08

n for the next, ? for upward

In a file where the same word appears many times, use n.

n    to the next match
/port -> ⏎    to the firstn              to the secondn              to the third

No typing the word again. n is next.

What happens at the last one is this.

press n at the last match  |back to the top, and searching again

It goes round and round. So keep pressing n and you circle the same places.

There is one for searching upward.

?word -> ⏎    search up
/    search down?    search up

Only the direction differs. Used when you are near the end of a file and want to go back.

A guide for choosing.

gg then /word     look from the top of the file in turnG then ?word      look backward from the end

Go to an end and then search and you certainly see them from the first. / is "below where you are", so from the middle you miss the earlier ones.

Here is the real way of using it.

vi settings.confgg          back to the top/port -> ⏎  to the first port(mend it)n           to the next port(mend it)n           to the next port

Search, mend, next, repeated. This may be the commonest use of vi in practice.

Put with the . of an earlier lesson, it gets faster still.

/port -> ⏎  jumpdd          removen           next.           remove the same way

Search and repeat. The form for the same mend in several places.

04 / 08

Replace with :s

Searched and jumped, you mend. But for "the same mend in twenty places", you want it done together.

Here is the command for it.

:s/old/new/
port 80  | :s/80/8080/port 8080

Typed after :. The same way in as :wq and :3.

How to read it.

:s      substitute/80/    what to look for/8080/  what to put instead

Divided by slashes. Written exactly like the sed of chapter 5.

sed 's/80/8080/' filethe :s/80/8080/ of vi

The s part is the same. So anyone who did chapter 5 has seen it before.

:s alone applies to this line only.

:s/80/8080/     only the line you are on

And only the first one in the line.

a a  | :s/a/Z/Z a          <- the second a stays

To change all of them in the line, add a g at the end.

:s/a/Z/g  |Z Z

g is global. The same as sed again.

To gather up.

:s/A/B/      the first one in this line:s/A/B/g     all of them in this line

Whether you add the g is what divides them. Forget it and only one changes, so get into the habit of checking the result.

05 / 08

Replace the whole file with :%s

Name lines and you choose the range.

:%s/A/B/g     the whole file:2,3s/A/B/    lines 2 to 3:1,$s/A/B/    line 1 to the last (the same as %)

% is the mark for "every line".

:%s/80/8080/g

This is the most used shape. It turns every 80 in the file into 8080.

Mind that the result changes with the g.

a aa a
What you typeResult
:%s/a/Z/two lines of Z a (the first in each line)
:%s/a/Z/gtwo lines of Z Z (all of them)

% is "which lines", g is "how many within a line". They say different things.

%    the range of linesg    the range within a line

Do not mix them. When you need both, add both.

The shape that narrows the range is worth knowing too.

:2,3s/x/Y/     lines 2 to 3 only:1,10s/x/Y/g   all of them in lines 1 to 10
you do not want the comments changedthe first ten lines are a template you must not touch

It tells when there are places you do not want changed. sed can do the same, but with vi you decide while looking at the screen.

Lastly, the most important thing.

> Having replaced, always check

:%s/80/8080/g  | once you are outgrep -c 8080 settings.confgrep -c '80$' settings.conf

The number changed, and the number not left behind. A command that replaces everything at once is hard to notice when it goes too far. So you pin it down with numbers.

06 / 08

Replacing everything is dangerous

:%s is powerful, and its accidents are large too. Let us look at the common failures.

:%s/80/8080/g

What if the text 8080 were already there?

port 8080  | there are two 80s (inside the 8080)port 80808080

Unexpected places change.

Here is another failure.

:%s/log/LOG/g
log xdialog y      <- this changes too!

It matches part of a word. Do you remember minding the same thing with grep (chapter 5)?

There are three ways of guarding.

1. look at the count with grep first2. try it on a narrow range3. take a copy before you replace

The first is this.

grep -c 80 settings.conf     count how many there are first

Know the number that will change beforehand. If grep -c 8080 comes to the same number afterwards, you know it went as you meant.

The second is this.

:1,5s/80/8080/g    five lines first(look):%s/80/8080/g      all of it if it is good

Try small, then widen.

The third is the usual manner.

cp settings.conf settings.conf.bak

A copy before you touch.

And keep a way out for when you notice a failure.

u       cancel the last :%s:q!     throw it all away

u undoes a :%s too. Hundreds of places changed come back in one move.

:%s/.../.../g changed everything  | oh nou brings it all back

Before you save, you are safe. So having typed :%s, look at the screen first, and then :wq. Not typing :wq in a hurry is the best guard of all.

07 / 08

Choosing between grep, sed and this

Come this far and the same things can be done with two tools. Let us sort them out.

grep -n port settings.conf   see in a list which lines have itthe /port of vi              be taken to that line
sed -i 's/80/8080/g' file    replace them allthe :%s/80/8080/g of vi      replace with it open

Either will do. So how to choose?

just looking, want a list   grepwant to go and mend         the / of vi

grep shows you all of it at once.

grep -n port settings.conf1:port 803:port 805:port 80

Three places, seen at a glance. Then you can go round with 1G, 3G and 5G in vi.

learn the places with grep -> go and mend with vi

Putting the two together is how it goes in practice.

The same for replacing.

all may change, and there are many   sed -iwant to choose, want to watch        vi

sed -i is fast, but you do not see the result. With vi you confirm on screen before saving.

sed -i    fast, no going backvi        slower, :q! goes back

Whether it can be taken back is the difference.

So decide like this.

an important file, not confident   vi, watchingthrowaway, confident               sed -i, all at once

Until you are used to it, vi is what I would suggest. With :q! there, a failure does not become a failure.

And you have both already. Being able to choose is the strength. Knowing only one, you end up doing only what that tool can do.

08 / 08

Now vi is usable

To gather up. Searching and replacing are in place.

/word n   search down, to the next?word     search up:s/A/B/   this line:s/A/B/g  all of them in this line:%s/A/B/g the whole file:2,3s/... narrow the lines

You can find a place by its contents. You mend without knowing the line number.

Over six lessons, all of vi has gone in.

lesson 1  in and out   Esc :q :q! :w :wq ZZlesson 2  write        i a I A o Olesson 3  move         h j k l 0 $ w b gg G 3Glesson 4  remove       x dw D dd r cw u .lesson 5  copy         yy yw p P (dd p to move)lesson 6  search       / ? n :s :%s

With just this, the work of the field is all covered. There are handier commands, but you will not miss them.

Here are the most important ones once more.

Esc      press it when in doubt:q!      whatever you do, back as it wasu        cancel the last thing

Those three let you touch it boldly. You will never be shut inside vi again.

The next lesson is the test of skill.

put together what thirteen chapters taught you, with no instructions

Problems that do not say "open vi" or "use grep". You decide what to use and how.

find files          find greplook at contents    cat head lessmend                virun                 chmod ./scriptcheck               wc grep diff

All of them your tools. What you have stacked up since chapter 1, used all at once at the end.

Solve it and you need never fear a terminal again. Look forward to it.