[comp.editors] 2 more macros that change the case of a word

bink@aplcen.apl.jhu.edu (Ubben Greg) (04/24/89)

In article <175@inf.ethz.ch> wyle@ethz.UUCP (Mitchell Wyle) writes:
> ...
>Consider the "transpose case macro" discussion with such gems as:
>
>map V :s/\([a-z]*\)\([A-Z]*\)/\U\1\L\2/g^V^M
>map v mxdwO^VpV0D:d^V^M`xP`x
>
>and
>
>map ^T i^M^[Ea^M^[-y$Pa^M^[-:s/./\~/g^M"zd$+@z--4J
>map ^T i^M^[Ea^M^[-y$Pa^M^[-:s/^[   ]*//^M:s/./\~/g^M0"zd$+@z--4J
> ...


Here are several more vi macros I've written to change the case of word(s).
Both affect letters from the current cursor position to the end of the word
(as "cW" does), then advance to the beginning of the next word (as "W" does).
Install the one you wish to use in your .exrc, replacing ESCAPE and RETURN
with the corresponding control characters, and invoke by pressing "=".

map = mzdW$mypylp`yl~~~~~~~~~~~~~~~~~~~~$x`ylD`zPW
map = mzi<{ESCAPEbEa}>ESCAPE:s/<{\(.*\)}>/\U\1RETURN`zW

The first version uses the ~ command to toggle the case of the letters.  If
you exchange the initial mz and dW commands then it will take a repeat factor
to convert multiple words (e.g. "5="), but will change at most 20 characters,
will crash if this spans a line end, and will no longer work on the last word
in the line.  I decided that speed was more important than using the trick
in the above gems which avoids the 20-character limit.

The second version uses the \U substitute replacement flag to convert letters
to uppercase.  It is more robust, but seems to run a little slower.  It also
fails on a null line sometimes, as (at least this version of) vi has a bug
whereby using w or W to get to a null line causes a following i command on
that null line to screw up (another bug is, w won't advance past a line ending
in white space).  To make the corresponding macro to convert a word to
lowercase, simply change \U to \L and give it a different name (such as -).

Please use E-mail for comments, or if you wish further explanation.
Does anybody know how to write a macro that takes any vi object as an argument?
For example, =w, =5j, =H, =/pattern^M, etc.  I don't think it can be done in
one command, though it should be possible as <macro1><object><macro2>.
For example, =w+, =5j+, =H+, =/pattern^M+

BTW, I just realized that most of the mail I've sent from this (Ultrix-32 V3.0)
system has been incomplete, because this is the stupid version of vi that
doesn't save the file on a ZZ if you've just done a :w to another file.
I apologize.  Looks like I've wasted a lot of my time.

						-- Greg Ubben
						   aplcen.apl.jhu.edu