Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with newline character while using ansi-term in emacs

I am trying to use ansi-term in emacs (configured to tcsh shell). I see some issues with newline characters being displayed. If I try the following from the terminal (ansi-term), I get the correct output:

myterm > echo "Line1"; echo "Line2"; echo "Line3";    
Line1
Line2
Line3
myterm >

But if I try putting the same lines in a shell script and try to execute the script from ansi-term, I get a wrong output

Script: (test)

#!/usr/bin/env tcsh
echo "Line1"; echo "Line2"; echo "Line3";

Running the script (test):

myterm > ./test
Line1
     Line2
          Line3
               myterm >

Note: /usr/bin/env tcsh does point to the correct shell (its the same shell that I used while invoking ansi-term). Also executing the script from gnome-terminal also displays the correct output. I have also tried setting the following variables but it did not solve my issues:

(set-terminal-coding-system 'utf-8-unix)
(setq default-process-coding-system '((utf-8-unix . utf-8-unix)))
like image 598
Pulimon Avatar asked Apr 25 '16 21:04

Pulimon


1 Answers

If you set stty onlcr in your script, you will get the behaviour you require.
Translating the command into english one might say:
set the tty to output newline as carriage-return and newline.

This is a workaround of course, because this option should be set by default. I can see from the output of stty -a that you gave in your comments that it is set in the tcsh that runs in your ansi-term. I suspect one possible reason why ansi-term and your shell script behave differently is due to the following lines in term.el

    (apply 'start-process name buffer
       "/bin/sh" "-c"
       (format "stty -nl echo rows %d columns %d sane 2>/dev/null; 
                if [ $1 = .. ]; then shift; fi; exec \"$@\""
               term-height term-width)
       ".."
       command switches)))

The stty command in the above actually sets onlcr twice, since
the compound option -nl translates to icrnl -inlcr -igncr onlcr -ocrnl -onlret
and the sane option translates to
cread -ignbrk brkint -inlcr -igncr icrnl -iutf8 -ixoff -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

Another possible cause: for non-login shells tcsh will only read /etc/csh.cshrc and either ~/.tcshrc or ~/.cshrc when it starts up, but for login shells it reads a number of other files including /etc/csh.login ~/.history or the value of $histfile - You should consult the man page for full details including the exact order in which it reads things.

like image 111
Niall Cosgrove Avatar answered Nov 14 '22 05:11

Niall Cosgrove