Ukelele or keylayout files can't be used to change modifier keys to other keys. But you could use a private.xml like this with KeyRemap4MacBook:
<autogen>__KeyToKey__ KeyCode::COMMAND_R, KeyCode::ESCAPE</autogen>
This would only change pressing the right command to escape:
__KeyOverlaidModifier__ KeyCode::COMMAND_R, KeyCode::COMMAND_R, KeyCode::ESCAPE
<only>VI, TERMINAL</only> would apply the settings only when MacVim, Terminal, or iTerm is frontmost.
See the source for the key code values and predefined settings.