6

我正在尝试在 emacs 中使用 ansi-term(配置为 tcsh shell)。我看到显示换行符的一些问题。如果我从终端(ansi-term)尝试以下操作,我会得到正确的输出:

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

但是,如果我尝试将相同的行放在 shell 脚本中并尝试从 ansi-term 执行脚本,我会得到错误的输出

脚本:(测试)

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

运行脚本(测试):

myterm > ./test
Line1
     Line2
          Line3
               myterm >

注意:/usr/bin/env tcsh 确实指向正确的 shell(它与我在调用 ansi-term 时使用的 shell 相同)。同样从 gnome-terminal 执行脚本也会显示正确的输出。我也尝试设置以下变量,但它没有解决我的问题:

(set-terminal-coding-system 'utf-8-unix)
(setq default-process-coding-system '((utf-8-unix . utf-8-unix)))
4

1 回答 1

15

如果您stty onlcr在脚本中设置,您将获得所需的行为。
将命令翻译成英文可能会说:
s et tty以输出新行作为c arriage- r returnnewline 。

这当然是一种解决方法,因为默认情况下应设置此选项。我可以从stty -a您在评论中给出的输出中看到它在您的 ansi-term 中运行的 tcsh 中设置的。我怀疑 ansi-term 和您的 shell 脚本行为不同的一个可能原因是由于 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)))

上面的stty命令实际上设置onlcr了两次,因为
复合选项-nl翻译成 icrnl -inlcr -igncr onlcr -ocrnl -onlret
sane选项翻译成
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

另一个可能的原因:对于非登录 shell,tcsh只会在启动时读取/etc/csh.cshrc~/.tcshrc,但对于登录 shell,它会读取许多其他文件,包括或值- 您应该查阅手册页以获取完整的详细信息,包括确切的它读取内容的顺序。 ~/.cshrc/etc/csh.login ~/.history$histfile

于 2016-05-04T21:23:02.647 回答