[comp.editors] Query-replace on VI

choy-albert@CS.YALE.EDU (Albert M. Choy) (04/26/88)

  Does anyone know if VI has the query-replace function?  I think
  I saw it posted here before. I've been hoping to find out for
  a long time. Thanx.

----------------------------------------------------------------------
choy@yale.arpa             Albert Choy (The Choyster)
choy@cs.yale.edu           Box 1305 Yale Station  New Haven, CT. 06520
choalbm@yalecs.bitnet        birth : school : Moria : work : death

wcs@skep2.ATT.COM (Bill.Stewart.<ho95c>) (04/29/88)

In article <27909@yale-celray.yale.UUCP> choy-albert@CS.YALE.EDU (Albert M. Choy) writes:
>  Does anyone know if VI has the query-replace function?  

Two ways to do it, both from the manual:
1)	/foo	# search for foo
	cwBAR	# replace it with BAR
	n	# find next foo		### repeat these two steps
	.	# do cwBAR again	### many times.

2)	:g/foo/s//BAR/gc		### works in ex-mode
	
-- 
#				Thanks;
# Bill Stewart, AT&T Bell Labs 2G218, Holmdel NJ 1-201-949-0705 ihnp4!ho95c!wcs
# skep2 is a local machine I'm trying to turn into a server.  Please send
# mail to ho95c or ho95e instead.  Thanks.

ken@aiva.ed.ac.uk (Ken Johnson) (05/04/88)

>>  Does anyone know if VI has the query-replace function?  

>2)	:g/foo/s//BAR/gc		### works in ex-mode

Works, but it is not a query-replace; it is a global replace.
As far as I can see there is no way to do a query-replace in `vi',
but if that is so it's a serious omission.
-- 
------------------------------------------------------------------------------
From Ken Johnson, AI Applications Institute, The University, EDINBURGH
Phone 031-225 4464 ext 212
Email k.johnson@ed.ac.uk

mpatnode@polyslo.UUCP (Mike Patnode) (05/04/88)

In article <393@aiva.ed.ac.uk> ken@uk.ac.ed.aiva (Ken Johnson,E32 SB x212E) writes:
>
>
>>>  Does anyone know if VI has the query-replace function?  
>
>>2)	:g/foo/s//BAR/gc		### works in ex-mode
>
>Works, but it is not a query-replace; it is a global replace.
>As far as I can see there is no way to do a query-replace in `vi',
>but if that is so it's a serious omission.
What are you talking about?  When I use the following command:

:g/is/s//isn't/c

On this article, I get a little line at the bottom of the screen like this:

Distribution: 
  ^^

Now if I type a y, followed by a return it will replace the occurance.
Anything else followed by a return (nothing included) will skip the
occurance.  Try it. Now if that isn't a query-replace then what is?
If your vi doesn't do this then you bought a lemon.

-- 
Mike "Dodger" Patnode          | (n)   ..csustan!polyslo!mpatnode 
Yitbos Innovations Inc.        | (s)   ..sdsu!polyslo!mpatnode 
244 California Blvd            |       mpatnode@polyslo.UUCP
San Luis Obispo, Ca  92630     | (805) 541-2048 / 543-9818 / 756-2516

lssabel@phoenix.Princeton.EDU (Laura Sabel) (05/05/88)

In article <393@aiva.ed.ac.uk> ken@uk.ac.ed.aiva (Ken Johnson,E32 SB x212E) writes:
<>>  Does anyone know if VI has the query-replace function?  
<
<>2)	:g/foo/s//BAR/gc		### works in ex-mode
<
<Works, but it is not a query-replace; it is a global replace.
<As far as I can see there is no way to do a query-replace in `vi',
<but if that is so it's a serious omission.

Yes, this works; it's a global query-replace.
The 'c' at the end makes vi ask you before each replace.


-Laura
rutgers!phoenix!lssabel

schwartz@gondor.cs.psu.edu (Scott Schwartz) (05/05/88)

In article <393@aiva.ed.ac.uk> ken@uk.ac.ed.aiva (Ken Johnson,E32 SB x212E) writes:
>
>
>>>  Does anyone know if VI has the query-replace function?  

Try this:

use "/from" to find your query
use ":s/from/to/" to replace it
then repeatedly use "/" to jump the the next instance of the from pattern, and 
":s" to change it.

-- Scott Schwartz     schwartz@gondor.cs.psu.edu    schwartz@psuvaxg.bitnet

wsmith@uiucdcsm.cs.uiuc.edu (05/05/88)

>/* Written 12:10 pm  May  3, 1988 by ken@aiva.ed.ac.ukBARn uiucdcsm:comp.editors */
>
>
>>>  Does anyone knowBARf VI has the query-replace function?  
>
>>2)	:g/foo/s//BAR/gc		### worksBARn ex-mode
>
>Works, butBARtBARs not a query-replace;BARtBARs a global replace.
>As far as I can see thereBARs no way to do a query-replaceBARn `vi',
>butBARf thatBARs soBARt's a serious omission.
>-- 

The posting that I have munged is false.  The c flag at the end of the
command makes it be a query replace.  It make the replacement, I answered
'y RETURN' to the prompts.  
The command I used was :g/ i/s//BAR/gc

Bill Smith	wsmith@a.cs.uiuc.edu	ihnp4!uiucdcs!wsmith

brianr@tekig4.TEK.COM (Brian Rhodefer) (05/06/88)

It may not be very glamorous, but when I want a "query-replace"ish
operation in vi, I find the first occurrence of the string with the
slash "/" character, and, if it's an occurrence I want to replace,
I replace it with a suitable "change" operation, e.g., "cwBAR<esc>".
Having done this, the "n" key finds subsequent occurrences, and the
"." key repeats the substitution, if desired.  If you elect NOT to
do the replacement on a particular occurrence, just hit "n" again.


If you count keystrokes, this method is just as efficient as a real
query-replace command would be.


Coping with vi's egregious lack of a single-keystroke witticism injector
(but just barely),

Brian Rhodefer

limes@sun.uucp (Greg Limes) (05/06/88)

In article <393@aiva.ed.ac.uk> ken@uk.ac.ed.aiva (Ken Johnson,E32 SB x212E) writes:
>somebody responds to ...
>>somebody asks
>>>  Does anyone know if VI has the query-replace function?  
>
>>2)	:g/foo/s//BAR/gc		### works in ex-mode
>
>Works, but it is not a query-replace; it is a global replace.
>As far as I can see there is no way to do a query-replace in `vi',
>but if that is so it's a serious omission.

Ken, look at the command more carefully. That trailing "c" tells vi to
ask the user about each replacement. Sounds amazingly like how EMACS
handles "query-replace".

Yes, I would like to get more people using EMACS ... but not by claiming
that VI lacks such an important feature.
-- 
   Greg Limes [limes@sun.com]			Illigitimi Non Carborundum

lewm@tekig5.TEK.COM (Lewis R. McCallum) (05/06/88)

In article <393@aiva.ed.ac.uk> ken@uk.ac.ed.aiva (Ken Johnson,E32 SB x212E) writes:
>
>
>>>  Does anyone know if VI has the query-replace function?  
>
>>2)	:g/foo/s//BAR/gc		### works in ex-mode
>
>Works, but it is not a query-replace; it is a global replace.
>As far as I can see there is no way to do a query-replace in `vi',
>but if that is so it's a serious omission.
>-- 
>------------------------------------------------------------------------------
>From Ken Johnson, AI Applications Institute, The University, EDINBURGH
>Phone 031-225 4464 ext 212
>Email k.johnson@ed.ac.uk

I don't know what version of `vi' you're using, but the above command
sure works on version 3.7 (type :ver to see what version you have).  In
general, the `g' suffix defines the subsitution as global; the `c'
suffix as query-replace (perhaps the `c' is for confirm?).  

When I want to do a query-replace on an entire file, I do    

		:%s/oldword/newword/gc 

Also, remember, `vi' is a line-oriented editor, so if you do
substitutions on phrases instead of a single word, `vi' will miss those
phrases that break accross the end of a line.  Hope this helps.

Lew 

towfigh@phoenix.Princeton.EDU (Mark Towfigh) (05/06/88)

In article <52205@sun.uucp> limes@sun.UUCP (Greg Limes) writes:
>>>>  Does anyone know if VI has the query-replace function?  
>>
>>>2)	:g/foo/s//BAR/gc		### works in ex-mode
>>
>Ken, look at the command more carefully. That trailing "c" tells vi to
>ask the user about each replacement. Sounds amazingly like how EMACS
>handles "query-replace".
>
>Yes, I would like to get more people using EMACS ... but not by claiming
>that VI lacks such an important feature.

The problem is not that vi doesn't have the feature, but how
difficult it is to remember the key sequence, and how slow it is to
key it in.  Just the fact that people had to post a query about how
to do this function is an indication of the dearth of on-line help
in vi.

I use Jove, an Emacs-based editor.  If I had not known how to do a
query-replace, I could have just searched for all commands with the
string "query" or "replace" in their names.  Then I would be able to
get a 3 or 4-line description of the command.

And when it comes to executing the command, here is my key sequence
(the spaces don't count):

            ESC-q search-string RET replace-string RET

and then after that, I have a myriad of options on each replace,
including "replace all subsequent occurences without asking", "pause
here and let me edit the string", "stop", etc.

If you add it up, you see that vi takes *6* more keys to execute the
same command, plus the command keys are less accessible (ESC and RET
for EMACS vs. "/" and ":" for vi).  And don't get me started on the
confusion between "ex-mode" and "insert-mode" in vi.

There's nothing in the promotion of EMACS for me; it's just that I
hate to see people waste time and get frustrated.

Mark Towfigh
-- 
Mark Towfigh       If there's one thing I like better than a bologna
                   and whipped cream sandwich, it's honey and ketchup.
=======================================================================
UUCP/Inet:         towfigh@phoenix.princeton.edu  BITNET:  TOWFIGH@PUCC

hansm@cwi.nl (Hans Mulder) (05/07/88)

In article <3536@psuvax1.psu.edu> schwartz@gondor.cs.psu.edu (Scott Schwartz) writes:
>In article <393@aiva.ed.ac.uk> ken@uk.ac.ed.aiva (Ken Johnson,E32 SB x212E) writes:
>>>>  Does anyone know if VI has the query-replace function?  
>Try this:
>use "/from" to find your query
>use ":s/from/to/" to replace it
>then repeatedly use "/" to jump the the next instance of the from pattern, and 
>":s" to change it.

When you get tired of hitting <return> every other keystroke, use
"n" to search forward to the next occurance of "from"
"N" to search backward
"&" to change it

--
Hans Mulder	hansm@cwi.nl	mcvax!hansm

limes@sun.uucp (Greg Limes) (05/07/88)

[in response to the wrangling going on about vi's "query replace",]

In article <2801@phoenix.Princeton.EDU> towfigh@phoenix.Princeton.EDU (Mark Towfigh) writes:
>The problem is not that vi doesn't have the feature, but how
>difficult it is to remember the key sequence, and how slow it is to
>key it in.

Difficult to remember? gag. I am assuming the guy who did not know about
the "check" feature of the substitute command had never seen it; the
form is similar to specifying "every instance on the line". As for time
to key it in, my search and replace patterns are large enough and I type
with more than two fingers, so the few spare characters do not slow me
down.

>           Just the fact that people had to post a query about how
>to do this function is an indication of the dearth of on-line help
>in vi.

So go to the off-line help; thats one of the things that makes VI small
and EMACS large. VI never claimed to have online help.  You want online
text, you pay for it; maybe in core, maybe in swap, always in disk.
Offline text costs you bookshelf space.

On a slightly different topic, since when did *this* exchange become a
battle of the better editors?  Personally, I use GnuEmacs, vi,
textedit, microemacs, tc, 1word, and edlin. Different tools for
different jobs and different environments.

Consider that some people may not have Emacs or Jove available on their
systems; for their sakes, lets keep the information level up and the
flamage level down.
-- 
   Greg Limes [limes@sun.com]			Illigitimi Non Carborundum

ken@aiva.ed.ac.uk (Ken Johnson) (05/09/88)

>>>>  Does anyone know if VI has the query-replace function?  

>>>2)	:g/foo/s//BAR/gc		### works in ex-mode

>>Works, but it is not a query-replace; it is a global replace.
>>As far as I can see there is no way to do a query-replace in `vi',
>>but if that is so it's a serious omission.
>
>Ken, look at the command more carefully. That trailing "c" tells vi to
>ask the user about each replacement. Sounds amazingly like how EMACS
>handles "query-replace".

You are all right -- it is a query replace. I didn't notice the
little `c' at the end. I looked in both the Vi manual and the Ex
manual, and then posted my own note. In future I will try to check
my brain is engaged before operating my mouth.

-- Ken
-- 
------------------------------------------------------------------------------
From Ken Johnson, AI Applications Institute, The University, EDINBURGH
Phone 031-225 4464 ext 212
Email k.johnson@ed.ac.uk

hansm@cwi.nl (Hans Mulder) (05/17/88)

In article <653@ubu.warwick.UUCP> nwho@diamond.UUCP (Nick (Alfie) Holloway) writes:
>What I would like to know is what other specifiers are there that can
>go on the end of a substitute command. I already know
>	g - global
>	c - query (check?)

	c - confirm
	p - print
	l - list
With %s/// both p and l print the last line affected, with g//s/// all
such lines are printed.

>And a final question, how do I copy a marked section. I can move using
>	:`a,`bm.
>(or whatever for addresses)
>but if I wish to copy do I have to yank then put?

The easiest to remember is
	:`a,`b copy .
The shortest valid abbreviation for copy is co, not c:
	:`a,`bco.
But you can save one more keystroke by using t:
	:`a,`bt.

Copy is one of those commands for which 0 is a valid line number,
indicating that you want the copy before line 1.

Hans Mulder	hansm@cwi.nl	mcvax!hansm

wsmith@uiucdcsm.cs.uiuc.edu (05/18/88)

You can copy any range of text with

:'a,'bco'c

where 'a, 'b and 'c represent marks, line numbers, /pattern/, ?pattern?, 
$ or . (% can be used instead of 'a,'b also).

Bill Smith	pur-ee!uiucdcs!wsmith	wsmith@a.cs.uiuc.edu

(Can the change directory command be trusted to work on all vi systems?
:chd /tmp
is an example.)