VI(1) vs. the world

gm at lmi-angel.UUCP gm at lmi-angel.UUCP
Sat Oct 18 06:55:40 AEST 1986


In article <3772 at umcp-cs.UUCP>, chris at umcp-cs.UUCP writes:
> >In article <113 at twg-ap.UUCP> narayan at twg-ap.UUCP (Narayan Mohanram) writes:
> >>How about :g/old-string/s//new-string/gc for conditional replace. Or is
> >>query replace something different.
> 
> I was ecstatic when I discovered a `confirm' (or `conditional')
> suffix to substitute operations in vi.  (This was before I ever
> saw Emacs.) Then I found that its implementation was rather flawed.
> Vi performs the conditional replace by:
> 
> 	1.  Printing the line.
> 	2.  Printing `^'s below the matched text.
> 	3.  Awaiting confirmation: y<RETURN> or n<RETURN> (others
> 	    presumably equivalent to `no').

There are a couple of `best ways' to do conditional substitution that
I've found.  The first step is to do a simple search for the pattern
that you are interested in replacing, using `/' or `?'.

Once you have located an instance of your target string, choose the
most convenient way to replace it.  There are several alternatives:

* Following vi's `operator-object' command paradigm, choose an operator
  that changes text (such as c, C, r, R, s, S) and an object of text
  (such as w, W, f, t, ), }) and make the appropriate change.

* Do a single-line substitution command, leaving the left-hand-side of
  substitution blank, thereby reusing your original search pattern.
  (e.g.  :s//bletch/)  Don't append a `g' for global substitute!

Now that you've made the first change, use `n' or `N' to find the next
occurrance of your pattern.  If you don't want to change this occurance,
simply issue another `n' or `N' and proceed to the next occurance.  If
you do wish to change this one, use vi's `redo' facility to make the
identical change with a single keystroke.  If you made the first change
with a single-line substitution command, repeat the substitution with a
`&', if you used an `operator-object' command, repeat it with a `.'.
-- 
-- Greg McGary        {decvax!cca,harvard,mit-eddie}!lmi-angel!gm
--
``Make something of yourself! ... Try a casserole!''



More information about the Comp.unix.wizards mailing list