rename , a built-in command, renames or removes a command.
To remove a command, rename it to the empty string:
rename foo {}
To rename a command to the empty string instead of removing it:
rename foo ::
To rename a command only if a command by the new name doesn't exist:
if {[namespace which newname] eq {}} { rename oldname newname }
DGP: Be aware that if you rename a command into another namespace, its evaluation context will change to that namespace.
% namespace eval one { variable message "I'm in one" proc test {} {variable message; return $message} } % namespace eval two {variable message "I'm in two"} % rename one::test two::test % two::test I'm in two
aspect: Also watch out for where you're renaming commands to:
% proc foo args {} % namespace eval ddd {namespace which -command foo} ::foo % namespace eval ddd {rename foo foo} % namespace eval ddd {namespace which -command foo} ::ddd::foo
HaO 2010-04-10: (discussion on CLT ): rename or delete from inside targeted proc is allowed:
proc ::runOnceAndDelete {} { # do something rename ::runOnceAndDelete "" }