4

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?

  • Have you tried M-x terminal-emulator? – jordanm Jan 19 '13 at 17:49
  • the display is even worse than when using M-x shell – Marcus Junius Brutus Jan 19 '13 at 20:34
  • You are right, the garbage is leftovers of ESC sequences for setting colors and such. – vonbrand Jan 21 '13 at 17:47
7

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
2

I think you'd better enable ansi-color in your shell-mode settings.

 (eval-after-load 'shell
   '(progn
      (autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
      (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on t)
      t))
0

Further building on DaveEmme's answer here's what I finally put in my .bashrc to keep things simple:

case "$EMACS" in
    t)
    PROMPT_COMMAND=
    PS1="[\u@\h:\w]$ "
esac

Relying on the value of $TERM didn't work in my case whereas Emacs is required to set the $EMACS environment variable to true for any shells it spawns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.