vi case-change summary (a bit long)

Rich Holloway rich at gtx.com
Fri Jul 7 09:34:31 AEST 1989


A few days back, I wrote:
} 
} In vi, I would like to use the '~' operation on more than one character
} at a time, but don't really want to take on the overhead of shelling out
} to "tr" or somesuch.  To wit, what I would like is a way to convert
} upper to lower from the cursor to the end of line.
} 
} Any ideas?

I got several responses via e-mail & news, thanks to all.  Here is a summary
of the answers:

*  In general, to convert a known string to upper/lower, you can use:

        s/<string>/\U&          (converts to upper)
        s/<string>/\L&          (   ''    '' lower)


*  For my specific question, I got this from sun!sunburn!cse.ogc.edu!schaefer:

    This will convert the current word to lower case (caveats below):

	map ~!w "cdei:s/\(^V^[pa\)/\L\1/^V^[F:Pl"cd3f/@c^V^M

    The ^V ^M and ^[ should be translated to control-V control-M and ESC when
    put in your .exrc file.  I chose ~! because they are close together on my
    Tek 4315 keyboard, you probably want to use something else.

    Caveats:  The cursor *must* be on the first character of the word before
    you type ~!w.  This must be the first occurrence of the word on the
    current line (otherwise you will change the earlier occurrence instead of
    the current one).  If this is the last word on the line, this does
    strange things, because in that case you need to append the ":" with "a"
    instead of inserting it with "i".  For the last word on the line use the
    macro below.

    To lowercase from cursor to end of line:

	map ~!$ "cd$a:s/\(^V^[pa\)/\L\1/^V^[F:Pl"cd3f/@c^V^M

    which will work UNLESS there is a '/' character in the text to be
    lowercased.  In this case, if there is a space preceding the text to be
    uppercased, you need to start with the cursor on the space.

    Neither of these work if there are regular-expression magic characters in
    the text to be lowercased unless you use :se nomagic.  They can also break
    on long lines if you have wrapmargin set greater than 0.

    The "c named buffer is used, so its contents will be wiped out if any.

    You can uppercase (instead of lowercase) by changing \L to \U in either.

    It's a bit slow at 2400 baud or less.

The second one isn't pretty, but it works like a charm.  I guess it just
goes to show that you can do almost anything in Unix if you just think
about it long enough.

Thanks again to all for the help.
-- 
Richard L. Holloway	...!sun!sunburn!gtx!rich      
GTX Corporation,        Phoenix, Arizona  



More information about the Comp.unix.questions mailing list