0

I make much use of sub-shells within Emacs (initiated via M-x shell), and, because of this, I run into problems like the one illustrated1 below way too often:

$ python -m pdb /tmp/hello_world.py 
[2] > /tmp/hello_world.py(1)<module>()
-> print 'Hello, world!'
^[[1A
(Pdb++) l
^[[1@l^[[9D
  1  -> print 'Hello, world!'
[EOF]
^[[1A
(Pdb++) quit()
^[[1@q^[[1@u^[[1@i^[[1@t^[[1@(^[[1@)^[[14D
$ 

In a regular terminal, the same interaction looks like this:

$ python -m pdb /tmp/hello_world.py 
> /tmp/hello_world.py(1)<module>()
-> print 'Hello, world!'
(Pdb) l
  1  -> print 'Hello, world!'
[EOF]
(Pdb) quit()
$ 

Whenever this sort of thing happens I resort to Google, and start trying the random fixes I find, more or less blindly, until one works, if any.

In this example, I've run python within an interactive debugger, all under a sub-shell, under Emacs (yes, it's quite a tower I've got here).

Here, Python's side of the interaction includes several escaped characters (preceded by ^[) that are not properly displayed.

Whenever this happens I resort to Google, and start trying the random fixes I find, more or less blindly, until one works, if any.

Is there a more systematic way to troubleshoot, diagnose, and (hopefully) fix such problems?


1In the display above, I've simulated things by rendering some characters the way they appear on my screen. Namely, I used two normal characters ^ and [ to display what is actually a single character, namely ASCII 27, aka ESC.

1
  • Your example works fine for me, running with no config (i.e., emacs -Q), running on Emacs 27.0.50. M-x shell supports ansi color codes and a lot of the escape sequences now, you might need to update your Emacs.
    – Tyler
    Commented Mar 20, 2018 at 13:30

2 Answers 2

1

The fix is simple, run applications emitting escape codes intended for terminal emulators in a terminal emulator, like M-x term. You might still run into yet to be supported escape codes, but those can be implemented by looking them up on http://invisible-island.net/xterm/ctlseqs/ctlseqs.html and adding the corresponding sequence to term-handle-ansi-escape.

0

Install https://github.com/jorgenschaefer/elpy

Then M-x elpy-shell-switch-to-shell

C-h v python-shell-interpreter-args if you want to add any arguments to interpreter.

"a more systematic way to troubleshoot, diagnose, and (hopefully) fix such problems" for newbie is to clone experts' instead of tweaking your own configuration from the scratch.

1
  • I disagree that cloning someone else's config is a more systematic approach to dealing with this. Adding code you don't understand makes debugging harder, not easier.
    – Tyler
    Commented Mar 20, 2018 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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