HELP converting filenames!

Tom Christiansen tchrist at convex.COM
Sun Oct 8 00:27:24 AEST 1989


In article <9754 at cadnetix.COM> rusty at cadnetix.COM (Rusty Carruth) writes:
|In article <5003 at omepd.UUCP> merlyn at iwarp.intel.com (Randal Schwartz) writes:
|>In article <9234 at pyr.gatech.EDU>, david at pyr (David Brown) writes:
|>| 
|>| Hiya.  I have a friend who has about 200 files in a directory that are all
|>| upper case.  They are data files that need to be in lower case, because
|>| his brain-dead program won't recognize upper case letters.  
|>
|>Yeah, in Perl, it'd be:
|>...
|>But, if you don't have Perl (shame on you!), here's the /bin/sh (et. al.)
|>solution:

I second the "shame on you"; lwall's rename script posted in reponse to
this subject thread was the most powerful, yet small and elegant, such 
thing that the I've ever seen.  The day I compiled perl was my last
day of writing disgusting sed/awk/tr/sort/sh monstrosities.

|Of course, if all else fails, do my favorite trick which I use
|whenever all else fails.  Make a script.
|
|First, go to the directory of interest and do 'ls -1 > doit'.
|Chmod 777 doit. Now, go edit 'doit' (for all you emacs haters
|out there, sorry but emacs seems to work better for what I'm about
|to suggest than does vi) and make a macro (sorry folks, this
|trick will pretty much require emacs) which adds a space or two
|to the end of the current line and then copies that line to its
|end (making a line like 'file' end up as 'file  file  ' - If
|you have a problem with the trailing space, make the macro
|a bit different), and then changes the added information to
|all lower case, then goes to the next line.  Now, execute 
|that macro 200 times.  Now go back and add the 'mv' command to
|the front of each line (another macro).  Don't forget
|your #!/bin/csh (or sh), close and save, execute it,
|and you are done.

Shame on *you* for spreading the Black Legend that vi isn't up 
to this kind of thing.  The easiest way to do it is like this
(after directing your ls into a file and calling ex or vi on it):

:%s/.*/mv & \L&/

but you CAN write a macro for it.  for example, here is a centering macro:
(all these have been run through 'cat -v' to quote control characters)

map v $ma81a ^V^[81^V|D`ald0:s/  / /g^V^M$p

which is a better way to do the more convoluted:

map v :co.^V^Mk:s/./ /g^V^Mo^V^[80a ^V^[:-1s;.*;:s/&//;^V^M"mdd at m:s/../ /g^V^MJ

which is a better way to do the still more convoluted:

map v o^V^[k:co.^V^M:s/./ /g^V^Mo^V^[80a ^V^[:-1s;^;:s/;^V^M:s;$;//;^V^M"mdd at m:s/\(.\)./\1/g^V^M:s;^;:-1s/^/;^V^M"mdd at mjdd

The more complicated examples make use of buffer execation (eg. @a).  In
developing more elaborate macros or substitutes (like those with \(...\)
constructions in them), I often type the line in by hand and then yank it
into a buffer and execute the buffer.  That way if it fails, you can just
edit the line and try again.  You'll find that if you execute a buffer with
:map in it that you may need one more level of quoting via ^V than you 
would from your startup file.  Thus the above maps work as written from
your .exrc but if executed out of a buffer, they need doubled ^V's. 

You can also write mutually recursive macros.

I append my current map set from my .exrc for the stout of heart.
read the comments here for descriptions of what they do.  Some
of these may have extra ^V's cause they were developed using the buffer
execution method, but it doesn't seem to hurt anything when read
from the .exrc file. Yes, some of the macros define their own new
macros.

The "meta-key" I use to get at all the \c things is either tab OR backslash
depending on the whims of my fingers.

The lint and commenting macros are possibly the neatest things here.


"----- start of .exrc, after munging control characters w/ "cat -v"
"
"	INPUT MACROS that i always want active
"
map! ^Z ^[:stop^M
"	so i can stop in input mode.  note that autowrite is set, so 
map! ^A ^[:stop!^M
"	will stop me without writing.
"
map! ^K ^V^[O
"	lets me do kindof a negative carriage return in input mode.
map! ^B ^[bi
"	non-destructive ^W
map! ^F ^[Ea
"	and its inverse
"
"	ARROW MACROS next four let arrows keys work in insert mode; 
map! ^V^[OD ^V^[i
map! ^V^[OB ^V^[ja
map! ^V^[OA ^V^[ka
map! ^V^[OC ^V^[lli
"
"
"	EXCHANGE MACROS -- for exchanging things
"
map v xp
"	exchange current char with next one in edit mode
map V :m+1^M
"	exchange current line with next one in edit mode
map! ^P ^V^[hxpa
"	exchange last typed char with penultimate one in insert mode
map = ^^
"	edit previously editted file
"
"
"
"	other macros
"
map g G
"	because it's easier to type
map ^A :stop!^M
"	unconditional stop
map ' `
"	so we return to exact position, not just ^ on 'a or ''
map Y y$
"	so Y is analagous to C and D
map ^R ddu
"	single-line redraw
map ^N :n +/^M
"	go to next file in arg list, same position 
"	useful for "vi +/string file1 file2 file3"
"
"
"	META MACROS, all begin with meta-key '\' ; more later in file
"
map ^V	 \
"	so can use both ^I and \ for meta-key
"
map \\ i^M^[
"	split line
"
map \/ dePo/\<^V^[pA\>^V^["wdd at w
"	find current word, uses w buffer
"
map \w :w^M
"	write out the file
"
map \C $ma81a ^V^[81^V|D`ald0:s/  / /g^V^M$p
"	center text
"
"	EXECUTION MACROS --	these two are for executing existing lines.  
"
map \@ ^V^["mdd at m
map \2 \@
"	xqt line as a straight vi command (buffer m, use @@ to repeat)
map \! 0i:r!^V^["ndd at n
map \1 \!
"	xqt line as :r! command (buffer n, use @@ to repeat)
"	
"
"	BLOCK MACROS -- these make { and } hot keys in insert mode
"
map! ^O ^V^V^V{^M^V^V^V} ^V^[O^T
"	this will begin a block, leaving in insert mode
map! ^] ^V^[/^V^V^V}^V^Ma
"	and this  will take you past its end, leaving in insert mode
"
"
"
"	LINT MACRO.  deletes all text from "lint output:" and below, (if any)
"	replacing it with lint output in pretty block comment form.  could
"	do sed work myself, but this is faster.  
"
"	the map! is for subsequent map, not by people, 
"	tho /^Lo would make sense.
"	this is one of those famous time/space tradeoffs 
map! ^Lo lint output
"
"	and now for the real work
map \l Go^M/* ^Lo^M^[/^Lo^MdG:w^Mo/*** ^Lo^[<<:r!lint -u -lc %^V|sed 's/^/ *  /'^MGo***/^[N
"
"	indent this for me
"
map \i :%!indent -i4^M
"
"	COMMENTING MACROS -- these are actually pretty amazing
"
"	from edit mode, this comments a line
map ^X ^i/* ^[A */^[^
"
"	and this undoes it
map ^Y :s/\/\* \([^*]*\) \*\//\1^[
"
"	this next one defeats vi's tail-recursion defeatism
"	called by 2 maps following this one 
map! ^N ^V^[:unmap! ^V^V^M^[
"
"	while in insert mode, this will put you "inside" a comment
map! ^X ^V^[:map! ^V^V^M ^V^V^[a^V^V^V^No^[a /*  */^[hhi
"
"	while in edit mode, this begins a block comment -- ^N to escape
"	looking quite like this:
"	/*
"	 *  (you are here)
"	 */
"	(you were here)
map \c	O/*^M *  ^M*/^[k:map! ^V^V^M ^V^V^M*  ^MA
"
"	and this is for adding more lines to a block comment -- ^N to escape
map \o	:map! ^V^V^M ^V^V^M*  ^MA
"
"----- end of .exrc, after munging control characters w/ "cat -v"


So remember: 
    To a true hacker, all things are possible, but not all expedient. :-)


--tom

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist at convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"



More information about the Comp.unix.wizards mailing list