VI(1) vs. the world

Chris Torek chris at umcp-cs.UUCP
Thu Oct 16 01:04:40 AEST 1986


>In article <3772 at umcp-cs.UUCP> I wrote:
[Vi's :g/old/s//new/gc is not so wonderful.  Incidentally, :%s/old/new/gc
is equivalent, and no doubt faster.]

In article <85 at lmi-angel.UUCP> gm at lmi-angel.UUCP (Greg McGary) writes:
>There are a couple of `best ways' to do conditional substitution that
>I've found. ...

>* 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!

The former is better; here is why:

>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 `.'.

`&' repeats the most recent `:s' substitution.  This is a line-oriented
command, so if the pattern (or string, if you have set nomagic) appears
twice in a line, and you want to change the second occurrence, you are
out of luck.

Incidentally, vi's `c'hange, `d'elete, and other `directed' commands
are not inherently line or character oriented.  If you type, e.g.,
`cw', vi performs a character-oriented change: one word on the
current line.  If you type `cj', vi performs a line-oriented change:
the current and next lines, in their entirety, are replaced.  For
something that is this hard to explain, it seems remarkably intuitive.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1516)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix.wizards mailing list