Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!cuae2!ltuxa!we53!sw013b!dj3b1!killer!ndmce!pollux!infotel!ut-ngp!ut-sally!seismo!think!mit-eddie!genrad!decvax!cca!lmi-angel!gm From: gm@lmi-angel.UUCP@ndmce.uucp (Greg McGary) Newsgroups: net.unix-wizards,net.unix Subject: Re: VI(1) vs. the world Message-ID: <283@ndmce.uucp> Date: Fri, 17-Oct-86 16:55:40 EDT Article-I.D.: ndmce.283 Posted: Fri Oct 17 16:55:40 1986 Date-Received: Tue, 21-Oct-86 05:34:49 EDT References: <4327@brl-smoke.ARPA> <357@maynard.UUCP> <113@twg-ap.UUCP> <3772@umcp-cs.UUCP> Sender: news@ndmce.uucp Organization: LISP Machine, Inc; Cambridge Engineering Headquarters Lines: 41 Xref: watmath net.unix-wizards:20007 net.unix:9614 Summary: The best ways to do conditional substitution in vi(1) In article <3772@umcp-cs.UUCP>, chris@umcp-cs.UUCP writes: > >In article <113@twg-ap.UUCP> narayan@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 or n (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!''