Emacs shell mode (M-x shell)

emacs

When I am using the emacs M-x shell mode I see strange characters on my screen which I believe are related to my prompt (which includes an End-Of-Line) and the use of colors.

So, here's what I see on the terminal:

mperdikeas@thorin:~#
$ 

And here's what I see inside Emacs shell buffer:

^[]0;mperdikeas@thorin: ~^Gmperdikeas@thorin:~#
$  

Here's the relative section of my .bashrc:

if [ "$color_prompt" = yes ]; then
  PS1='${debian_chroot:+($debian_chroot)}\[\033[1;32m\]\u@\h\[\033[00m\]:\[\033[1;33m\]\w\[\033[00m\]#\n$'
else
  PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w#\n$ '
fi 

How can I configure emacs to properly display the prompt in the shell buffer?

Best Answer

There's probably a more elegant solution, but this works for me. After any prompt-related lines in .bashrc, insert the following, adjusting the value of PS1 to suit:

# Keep it simple if running in emacs.
case "$TERM" in
  dumb)
    PROMPT_COMMAND=
    PS1="\u@\h:\W$ "
esac
Related Question