Changing upper/lower case strings

Chris Torek chris at mimsy.UUCP
Mon Apr 17 12:04:27 AEST 1989


In article <911 at altos86.UUCP> mario at wjvax.UUCP (Mario Dona) writes:
>Does anyone know if vi can change a word from uppercase to lowercase
>(or vice-versa) with one command?

Yes, but it is not easy.

The `substitute' command has two special codes in replacement strings.
`\U' means `make upper case'; `\L' means `make lower case'.  Thus,
on any one line, the command

	:s/.*/\U&/

makes that line all upper-case.

The trick to make this work for a single word is to make a macro
that puts the word on a line by itself, does the appropriate `:s'
command, then joins the word back into the original lines.  It is
too hard to pick an individual word out of a line of text using
`:s'.

Unfortunately, there is no perfect way to split and splice lines.
Vi puts in its own notion of spacing.  Ignoring this problem, one
finds that the mapping

	lbi^M^[ea^M^[k:s/.*/\L&/^MJkJ

almost works.  It goes wrong at the end of the last word on the
last line, where the `l' produces an error.  Well, you can always
back up one character, unless it is the word `a' or `i'; but then
you can use `~'.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.unix mailing list