Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I try to make the Python shell get color:

python 2>&1 | source-highlight --failsafe -f esc -s python

the >>> prompt seems to be always delayed by 1 command.

Why is this happening, and how do I fix this?

share|improve this question
Use ipython to get a colorful prompt, among other things. – Keith Jul 27 '11 at 3:16
@Keith: That colors the prompt, but not the code -- which I care about more. And it's like 70 MiB. :\ – Mehrdad Jul 27 '11 at 3:31
Oh, right. Well do what Caleb says, use a colorizing text editor or IDE and run it from there. The source-highlight is a stream filter and won't work very well for interactive sessions. – Keith Jul 27 '11 at 4:07
@Keith: Do you happen to know why it doesn't work well? I've already observed the fact that it doesn't work, but what I'm wondering about is why. – Mehrdad Jul 27 '11 at 4:42
usually these tools read whole lines at a time, at least if not more. The prompt does not have a line ending, so the filter is waiting for more input. If it's reading by line, it will be shown after the \n is seen after Python echoes the command. Also, for input the readline module is reading and echoing characters in raw mode, so you won't see those highlighted as you type. Since most what you enter is not hightlight, and a lot of the output is not actually in Python source syntax I don't see what the value of this is. – Keith Jul 27 '11 at 5:18

I don't think this will ever work quite that way. The source-highlight program is designed to operated on text input via files (or STDIN), but it only process things one line at a time. It was not designed to act as an output filter for an interactive program like the python shell.

You might consider using an editor that has real-time syntax highlighting and setting up some macros to execute the file in the python interpreter or looking for alternate readline environment wrappers for python, but I'm pretty sure the solution you were trying won't work so don't beat yourself up over it too much.

share|improve this answer
Any idea why this is happening, though? – Mehrdad Jul 27 '11 at 3:31
I already hinted at why ... that filter is designed to work on streams one line at a time. It is not equipped to evaluate anything short of a full line. When working at a prompt, a full line has not been completed until you hit enter, so you see the filtered results of that until the next line. Also, since the majority of the output from the console won't even be python code, you are likely to confuse the parser and loose much of the benefit anyway. The lines you input that get echoed back to you are python, but most of what gets generated and printed back out isn't going to be python at all. – Caleb Jul 27 '11 at 6:26
I can live with too much coloring more easily than I can live with black-and-white, so that's not an issue. :) Interesting problem with the lines, though... feels kind of weird to not be able to fix it. – Mehrdad Jul 27 '11 at 6:31
There is a fix. Use an editor (or IDE) that was designed to color syntax on the fly (most do these days) and use it to execute your test code rather than hanging out in a python shell. – Caleb Jul 27 '11 at 6:47
That's not a fix, it just bypasses the problem. The whole point of the terminal is that it's for a quick test -- if I need to launch a new editor just for this, that defeats the purpose. – Mehrdad Jul 27 '11 at 6:50

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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