5

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?

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

4 Answers 4

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
0
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.

0

Try enabling support for OSC (Operating System Codes) by putting the following in your init file and restarting Emacs:

(add-hook 'comint-output-filter-functions 'comint-osc-process-output)

For more info see the Shell Mode section of the Emacs manual with C-h i followed by m Shell Mode <RET>:

By default, Shell mode handles common ANSI escape codes (for instance, for changing the color of text). Emacs also optionally supports some extend escape codes, like some of the OSC (Operating System Codes) if you put the following in your init file:

(add-hook 'comint-output-filter-functions 'comint-osc-process-output)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .