Fix meta-keys that break out of Insert mode

From Vim Tips Wiki

On Unix-based systems, the Meta (Alt) key may not work in Vim. For example, in insert mode, pressing Meta-A (Alt-A) may exit to normal mode, then execute normal-mode commands. This can occur with non-GUI Vim under some terminal emulators – those which generate escape sequences for Meta-characters. The actually generated escape-sequences are <Esc>a .. <Esc>z.

You must manually configure Vim to recognize these escape-sequences as Meta-characters, see below. Terminal emulators which are known to generate these sequences for Meta-keys are: rxvt (unix), putty (PC), teraterm (PC). Vim expects characters 225-250 for Meta-keys.

Here's how to fix Meta-keys on the Vim side:

Check what your Meta-keys generate:

i<press Ctrl-V><press Meta-A>

If you see ^[a (that is, escape character followed by something), then add this snippet to your vimrc: