Use
renameto renamerename:rename rename &The
&can be any identifier;&just reminds me of "references" in C.Use the renamed
renameto renameset:& set =Again, the
=can be any identifier;=is just intuitive to me.Now, rename other commands that are worth renaming, e.g.
& regsub R & string S & while WA command is worth renaming if, given its length n, and occurrences k, k(n-1)-(n+4) > 0. Solving for k, the formula becomes
k > (n+4)/(n-1). Here's a reference table that makes it easy:length of minimum example(s) command occurrences ------------------------------------------------ 2 6 if (consider renaming to "?") 3 4 for, set (consider renaming to "=") 4 3 eval, expr, incr (consider renaming to "+"), info, join, proc, puts, scan 5 3 break, catch, lsort, split, subst, trace, unset, while 6 3 format, lindex, lrange, regexp, regsub, rename, return, string, switch 7 2 foreach, lappend, linsert, llength, lsearch, unknown . 2 lreplace . 2 continue . 2Next, compact frequently used subcommands like
= I index = L lengthso you can do things like
S $I $x 7 S $L $xSome obvious miscellanea:
- Consider using the ternary operator
?to compact multipleifs. lappendcan set the first element of a list if it doesn't yet exist (no need to initialize).- You can set arrays without using
array, e.g.set doesNotExist(7) 43. - You can use strings (
"a b c") instead of[list a b c]. - You can interpolate in strings like so:
foo${a}bar. - You can use
two\ wordsrather than"two words". (Remember in general that for contiguous strings without spaces, double quotes can be omitted!) - You can almost always rewrite
fors aswhiles to save a character or two, since awhilecan simultaneously check and increment while aforuses separate blocks.
- Consider using the ternary operator
For larger programs, here's a trick I thought of but neverhaven't yet applied:
proc unknown {c args} {eval [info commands $c*] $args}This emulates interactive command abbreviations! It costs 54 characters, but now you can use
jforjoin,spforsplit,stforstring,wforwhile, and so on.