Fixing Garbage Characters in the Emacs Shell Buffer

In many default Linux configurations the stock .bashrc config turns on colorization by default for many shell commands like ls and grep.

Commands that colorize their output can cause problems for the Emacs shell buffer:

wjstaige@joshstaiger:~$ ls ^[[0m^[[01;32m#.bash_profile#^[[0m ^[[01;34mMail^[[0m ^[[01;34mblosxom^[[0m ^[[01;34mdownloads^[[0m ^[[01;34mwebadmin^[[0m ^[[mwjstaige@joshstaiger:~$

If you're seeing this issue, try adding the following to your .emacs file:

;;; Fix junk characters in shell-mode
(add-hook 'shell-mode-hook