2

Say I have a simple perl program:

for my $i (0..10) {
    print "$i\r"
}

print "\n"

Now if I run the program through shell-command, instead of getting a single line with 10, I will see the following thing in the output buffer.

  0^M1^M2^M3^M4^M5^M6^M7^M8^M9^M10^M

My question is how can I make the output look like the way similar to shell output (i.e. \r moves to the begin of a line, further output overwrites previous one, and \n switches to a newline).

1

Emacs's echo area is not a regular interactive terminal that responds to \r; instead it collects all the output and presents it on one line. Anything sent to echo is also logged to the Messages buffer. Multi-line outputs to the echo area are instead displayed in a new buffer.

If you want to get just 10 as the response, then echo only the final result as follows:

for my $i (0..10) {
    $result=$i
}
print "$result"

On the other hand, if all output is needed with proper formatting, then replace \r with \n as shown here:

for my $i (0..10) {
    print "$i\n"
}
print "\n"

For more complex output with multibyte character output that is being mangled by the shell, you can tweak the

M-x universal-coding-system-argument

to specify command-specific encoding before running the shell-command. The shortcut for universal-coding-system-arugment is:

C-x RET c

Hope one of these helps.

0

My question is how can I make the output look like the way similar to shell output (i.e. \r moves to the begin of a line, further output overwrites previous one, and \n switches to a newline).

If you invoke the script via M-&(async-shell-command), then carriage returns are interpreted the way you desire by default. This behaviour is controlled by the variable comint-inhibit-carriage-motion:

comint-inhibit-carriage-motion is a variable defined in `comint.el'.
Its value is nil

Documentation:
If nil, Comint will interpret `carriage control' characters in output.
See `comint-carriage-motion' for details.

The reason asynchronous commands are displayed differently to synchronous ones is because the former receive comint-output-filter as their process filter, whereas output from the latter is not interpreted in the same way. If you think this should change, and someone hasn't already done so, you can submit a feature request via M-xreport-emacs-bugRET.

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.