[comp.editors] VI question, nth occurence

benoni@ssc-vax.UUCP (Charles L Ditzel) (01/24/88)

how does one find the nth occurence of a string in VI?
e.g. find the 70 ocurrence of the string "dog".
thanks

jamesp@dadla.TEK.COM (James T. Perkins;1149;92-725;LP=A;60eC) (01/29/88)

> From: benoni@ssc-vax.UUCP (Charles L Ditzel)
> Date: 24 Jan 88 06:01:53 GMT

> how does one find the nth occurence of a string in VI?
> e.g. find the 70 ocurrence of the string "dog".
> thanks

For all the niftiness of vi, my vi v3.7 doesn't accept a count to the
string search "/string<CR>" command [Da dumb visual bell just makes my
screen flash in fits of unhapiness].  Neither will "?" (search
backward), "n" (repeat last search) or "N" (repeat last search in
reverse) take a count.  I tried putting a search in a macro and giving
the macro command a count (ex: "15@a"), but my macro didn't accept a
count either.  :maps don't alleviate this problem either.

The only way I can think of is to write a simple vi macro.  To do this,
search for your pattern once (initializes the default search pattern for the
"n" command):

	/string<CR>

Then start writing a macro to search 69 more times.  Create an empty
line:

	o<ESC>

Then add n-1 "n"s into the line (suppose n = 70, as suggested above):

	69an<ESC>

Then yank this line (without the trailing newline) into a macro/text
buffer and delete it (we'll use buffer n for "next"):

	0"ny$dd

Now you can perform the search for the 69th-match of the pattern with your
"n" macro:

	@n

And do this as many times as you like, each time searching for the 69th
matching pattern (you could use

	n@n

to search for the 70th matching pattern).

The upshot of this is that vi doesn't accept counts for string searches,
and by all rights one ought to think it should.  After all, isn't a
string search just another kind of a MOTION, like w, }, h, j, k, l or
especially f?

I propose a challenge to the hacker's world out there: Find an easy way
to make vi 3.7 accept counts for a search, or emulate this feature in
less than my backdoor, 16-character approach (excluding original
pattern).


 _	___
| |    / _ \   James T. Perkins, jamesp@dadla.la.tek.com, (503)629-1149
| |__ | |_| |  Tektronix Logic Analyzers, DAS System Software, Disk Services
|____||_| |_|  MS 92-725, PO Box 4600, Beaverton OR 97075

Killed processes never die... They just go to the big Bit Bicket in the sky.