[net.unix] Vi query

bill@ur-cvsvax.UUCP (Bill Vaughn) (09/01/85)

Is there a way in 'vi' to search for the next instance of the
word in which the cursor is presently in using, say, a single
keystroke (obviously one could use the '/' and enter the word by hand) ?
If there is a way to use a named buffer in a search string then it
could be done I guess, but from my reading and testing I don't think it's
possible to do that. Are there any other possible ways short of hacking 'vi'?
Have I missed something obvious? Asssuming I have not, I feel this might
be a useful 'feature' to add to 'vi'.  Comments?

This would be a nice command to have while doing spelling corrections
in a file. The following alias puts one into 'vi' with the list
of 'misspelled' words appended. The cursor is on the last word.

alias sp 'spell \!* >> \!* ; vi + \!*'

Now one could hit that magic key, and each word in the list could be quickly
found and corrected. When the cursor comes full cycle (using the 'n' command),
the word could be deleted and the process repeated until the appended list is
exhausted.  That is as close to an automatic spelling correcter as I want
or need.

If you feel that any answers you may have would be useful to
everybody on the net, please post. Otherwise just send mail.

		Bill Vaughn
		Center for Visual Science
		UNIV. OF ROCHESTER
		{allegra,seismo,decvax}!rochester!ur-cvsvax!bill

rab@alice.UucP (Rick Becker) (09/02/85)

Bill Vaughn of the University of Rochester asked:

> Is there a way in 'vi' to search for the next instance of the
> word in which the cursor is presently in using, say, a single
> keystroke (obviously one could use the '/' and enter the word by hand) ?

His intention was to provide an aid to spelling correction.
In answer to his question, I am posting a
technique for interactive spelling correction that I originally posted
in Jan, 1982. (Wow, how time flies!)

--------------
It can be frustrating to perform spelling correction on a document using
a screen terminal.  The output of the spell command can be placed on a file
or viewed on the screen, but tends to get lost as the spelling errors
are corrected on the screen.
This note describes a way of using the vi editor to provide facilities to search
out spelling errors in a document, and to correct them using the full power
of vi.  It does not require the user to type in any of the misspelled words.
--------------

Include the following EXINIT line in your .profile file
(or modify your current EXINIT line or .exrc file)

EXINIT='map #1 Gi/\<^V^[A\>^V^["add@a|map #2 1G!Gvispell^V^['; export EXINIT

This line sets up function keys 1 and 2 to provide special spelling functions.
(The ^V and ^[ characters are ctrl-V and escape.
By changing the "map" commands in the EXINIT definition, it is possible
to use keys other than function keys 1 and 2 for these functions.)

When the user hits function key 2, the vi command
1G!Gvispell
is executed.
This pipes the entire file to the following "vispell" shell script:

tee /tmp/vis$$
echo SpellingList
spell /tmp/vis$$
rm /tmp/vis$$

The shell script returns the entire input file, with the word
"SpellingList" appended followed by the list of misspelled words produced
by the spell command.

Now function key 1 is usable.  Whenever function key 1 is pressed, the 
following vi commands are executed  (comments in parens)

G	(go to end of buffer)
i/\< esc	(insert a slash, begin word, terminate with escape)
A\> esc	(end of word, terminate with escape)
"add	(delete this line into buffer a)
@a	(execute buffer a)

Thus the last line of the buffer is changed into a search command, deleted
from the buffer, and executed.
The user will see the first line in the file containing the misspelled word,
can make changes with any vi commands, and can continue searching for
other occurrences of the word with the vi "n" command.
Once "n" gives "Pattern not found", function key 1 can be used to search
for another word.

When the word "SpellingList" is searched for, the spelling correction process
is finished.

It is probably a good idea for the user to look over the spelling list
at the end of the buffer prior to using function key 1.  Any correct
words can be deleted before beginning to search for the misspelled ones.
-- 

    Rick Becker  alice!rab  research!rab

cwd@cuae2.UUCP (Chris Donahue) (09/03/85)

After using the "/" command in VI to find the first instance of a string, using
the "n" command will find the next instance. (And of course "." will repeat the
last command issued).

Chris Donahue
AT&T Info. Sys.
Application Engineering

wcs@ho95e.UUCP (x0705) (09/03/85)

> Is there a way in 'vi' to search for the next instance of the
> word in which the cursor is presently in using, say, a single
....
> This would be a nice command to have while doing spelling corrections in a file.
(I'm using vi version 3.9 on System V Rel 2)
I tried to define a macro for this that would do the following:
	insert a /			i / ESC
	yank the WORD into buffer x	"xyW
	execute the buffer		@x
I put the string i/ESC"xyW@x into register g, and executed it with @g.
Unfortunately, I got an error message about "Can't yank inside global/macro",
and got kicked out of visual mode.  I didn't get the problem if g just
contained "xyW , but with the i/ESC at the beginning it dies consistently.

For a spelling fixer, I use sfix, which runs spell on a document, then goes
through it to find the flagged words, making suggestions for misspelled words.
-- 
## Bill Stewart, AT&T Bell Labs, Holmdel NJ 1-201-949-0705 ihnp4!ho95c!wcs

peter@graffiti.UUCP (Peter da Silva) (09/04/85)

> This would be a nice command to have while doing spelling corrections
> in a file. The following alias puts one into 'vi' with the list
> of 'misspelled' words appended. The cursor is on the last word.

Change it to:

alias sp 'spell \!* | tee /tmp/new$$ >> \!* ;\
		mv tags /tmp/old$$;\
		sed 's/^\(.*\)/\1	?\1?/' > tags;\
		rm /tmp/new$$;\
		vi + \!*\
		mv /tmp/old$$ tags'

> Now one could hit that magic key, and each word in the list could be quickly
> found and corrected. When the cursor comes full cycle (using the 'n' command),
> the word could be deleted and the process repeated until the appended list is
> exhausted.  That is as close to an automatic spelling correcter as I want
> or need.

Use the 'tag' command '^]', which uses the current word as a tag. Then, viola!
You have it. Thank you very much for showing *me* a great new application!

brown@nicmad.UUCP (09/05/85)

In article <442@cuae2.UUCP> cwd@cuae2.UUCP (-Chris Donahue) writes:
>After using the "/" command in VI to find the first instance of a string, using
>the "n" command will find the next instance. (And of course "." will repeat the
>last command issued).

Careful!  The way you worded the above, it sounds as if you can use the '.'
command to repeat the last 'n' command.  That is not so.  The '.' will repeat
the last text editing command.  If you are looking for a word like 'their'
and you wanted to find them and change them to 'there', you could first do a
'/their' to find the first one, then use the 'cwthere' command to change
the word.  The 'cw' is the 'change word' command.  Use the 'n' to find the
next word and use the '.' to change it to 'there'.  If you don't want to
change the word that it finds, just press 'n' again.

So, as you notice in the example, the '.' will repeat the 'cw' command,
not the 'n' search command.  A powerful feature that I use alot.
-- 

Mr. Video   {seismo!uwvax!|!decvax|!ihnp4}!nicmad!brown

itkin@luke.UUCP (Steven List) (09/11/85)

In article <183@graffiti.UUCP> peter@graffiti.UUCP (Peter da Silva) writes:
>> This would be a nice command to have while doing spelling corrections
>> in a file. The following alias puts one into 'vi' with the list
>> of 'misspelled' words appended. The cursor is on the last word.
>
>Change it to:
>
>alias sp 'spell \!* | tee /tmp/new$$ >> \!* ;\
>		mv tags /tmp/old$$;\
>		sed 's/^\(.*\)/\1	?\1?/' > tags;\
>		rm /tmp/new$$;\
>		vi + \!*\
>		mv /tmp/old$$ tags'
>[...]
>Thank you very much for showing *me* a great new application!

One small detail - where's the input to sed?  Also, the tags file needs
to include the file name.  This should therefore probably be a shell
script, as follows:

:
# spell - generate a list of misspelled words and find them
#
#  This script loops through each file named in its argument list
#  `spell'ing that file and adding to a tags file.  The files must
#  be processed individually in order to include the file name in
#  the tags file.
#
rm spelltags		# clear out the old, if any
for file			# get errors in each file
do
	spell $file | sed "s/.*/& $file ?\<&\>?" >> spelltags
done
mv tags tags.old
mv spelltags tags
sort +0 -1 -o tags tags
vi + $*

Thanks to both of you for finally helping me to understand the use and
power of tags!  The light shines!
-- 
***
*  Steven List @ Benetics Corporation, Mt. View, CA
*  Just part of the stock at "Uncle Bene's Farm"
*  {cdp,greipa,idi,oliveb,sun,tolerant}!bene!luke!itkin
***

peter@graffiti.UUCP (Peter da Silva) (09/26/85)

> One small detail - where's the input to sed?  Also, the tags file needs
> to include the file name.  This should therefore probably be a shell

Picky, picky. I never said I actually tried it. The input to sed is (obviously:
heh heh) /tmp/new$$. The idea is a good one, tho' (preen preen). Thanks for the
egoboo.