I basically use US keyboard for programming and Spanish for everything else, and I´m always switching keyboard layout when I change from browser to VIM or console.
In Linux (gnome), keyboard layout was a per application preference, that the window manager remembered automatically, so for every application, if you switched keyboard layout, when you focus back to that application the keyboard layout would change for you.
Is there any way of doing this in OSX Lion?