(tcsh)xterm标题栏的postcmd别名打破了较少的texteditor

时间:2022-09-03 00:18:55

I set up my tcsh xterm to update the titlebar on "postcmd" with the name of the last command that was run and the directory.

我设置了我的tcsh xterm来更新“postcmd”上的标题栏,其中包含运行的最后一个命令的名称和目录。

This is similar to what I had (minimal example to reproduce):

这与我的相似(重现的最小例子):

alias postcmd 'echo -n "\033]0;hello_world\007";'

别名postcmd'echo -n“\ 033] 0; hello_world \ 007”;'

(note that this alias is in my .cshrc file. If just I type this on the command line, than it works 100% properly)

(请注意,这个别名在我的.cshrc文件中。如果只是我在命令行上键入它,那么它比100%正常工作)

This successfully updates the xterm titlebar to say "hello_world" after every command that I run, except for less When I run less, I get the following terminal output:

这成功地更新了xterm标题栏,在我运行的每个命令后都说“hello_world”,除了less。当我运行较少时,我得到以下终端输出:

>less abc.txt ESC]0;hello_world^GESC]0;hello_world^GESC]0;hello_world^GESC]0;hello_world^GESC]0;hello_world^GESC]0;hello_world^GESC]0;hello_world^GESC]0;hello_world ^GESC]0;hello_world^GESC]0;hello_world^GESC]0;hello_world^GESC]0;hello_world^GESC]0;hello_world^G... (END)

>更少abc.txt ESC] 0; hello_world ^ GESC] 0; hello_world ^ GESC] 0; hello_world ^ GESC] 0; hello_world ^ GESC] 0; hello_world ^ GESC] 0; hello_world ^ GESC] 0; hello_world ^ GESC] 0 ; hello_world ^ GESC] 0; hello_world ^ GESC] 0; hello_world ^ GESC] 0; hello_world ^ GESC] 0; hello_world ^ GESC] 0; hello_world ^ G ...(END)

The file never actually opens, it just prints this garbage string and I have to CTRL-C to cancel it. Is the bell or escape character messing up the initialization of less somehow? Any idea how I can modify this to not break less? I could just as easily use another editor, but sometimes I need to share my console with other engineers for debugging and I don't want to confuse them if they choose to run less.

文件永远不会打开,它只打印这个垃圾字符串,我必须按CTRL-C取消它。铃声或逃脱角色是否会以某种方式搞乱初始化?知道我怎么能修改这个以减少损失?我可以轻松地使用另一个编辑器,但有时我需要与其他工程师共享我的控制台进行调试,如果他们选择少运行,我不想混淆他们。

This is the actual code I am using, which has the same issue as the simple hello_world example:

这是我正在使用的实际代码,它与简单的hello_world示例有相同的问题:

alias postcmd 'set HIST = `history -h 1`; printf "\033]0;%s\007" "xterm: $HIST @ $cwd ";'

=====EDIT===== Some additional info:

=====编辑=====一些额外的信息:

> alias less
> echo $LESS
LESS: Undefined variable
> echo $TERM
xterm
>less --version
less 382
Copyright (C) 2002 Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less

>tcsh --version
tcsh 6.13.00 (Astron) 2004-05-19 (x86_64-unknown-linux) options 8b,nls,dl,al,kan,rh,color,dspm,filec

=== MORE EDIT ===

===更多编辑===

On further debugging, I find that the problem only occurs when I put the command in my .cshrc file. If I uncomment the alias from .cshrc, and simply type the alias on the command line, then it works properly with less.

在进一步调试时,我发现仅当我将命令放在.cshrc文件中时才会出现问题。如果我从.cshrc中取消注释别名,只需在命令行中键入别名,那么它就可以正常工作。

Also, having put the alias in my .cshrc , if I unalias it, it still breaks less even after being unaliased. So it seems the problem is not coming from the presence of the alias, but from being aliased while .cshrc is being executed on terminal creation?

另外,将别名放在我的.cshrc中,如果我使用unalias,它甚至在未混淆之后仍然会减少。所以似乎问题不是来自别名的存在,而是来自于在终端创建时执行.cshrc时的别名?

2 个解决方案

#1


1  

Based in discussion in the comments, I don't know what's causing the problem, but I've come up with a workaround.

根据评论中的讨论,我不知道是什么导致了这个问题,但我想出了一个解决方法。

I'm unable to reproduce the problem myself, even with tcsh 6.13.00 (which is fairly old, BTW).

我自己无法重现问题,即使使用tcsh 6.13.00(相当陈旧,BTW)。

But creating the alias from .login, or from a file sourced from .login, seems to avoid the problem.

但是从.login或源自.login的文件创建别名似乎可以避免这个问题。

In my own experiments, I aliased postcmd to a command that updates the xterm title bar with a high-resolution timestamp. With the alias defined in my .cshrc, the title bar updates rapidly as my shell starts, indicating that postcmd is executed even while init scripts are being executed, before the first interactive prompt appears. (Aliasing postcmd to something that appends information to a log file could help track this down.)

在我自己的实验中,我将postcmd别名为一个命令,该命令用高分辨率时间戳更新xterm标题栏。使用我的.cshrc中定义的别名,标题栏会在我的shell启动时快速更新,表示即使在执行init脚本时,也会在第一个交互式提示出现之前执行postcmd。 (将postcmd别名化为将信息附加到日志文件的内容可能有助于跟踪此情况。)

Since .login is sourced after .cshrc, moving the alias definition to .login means that fewer commands are executed with the alias in effect. That's not a bad idea anyway; you probably don't need the alias other than in a login shell.

由于.login源自.cshrc,因此将别名定义移动到.login意味着使用别名执行的命令更少。无论如何,这不是一个坏主意;除了在登录shell中,你可能不需要别名。

My guess is that something in one of your startup scripts interacts with your postcmd alias in a way that messes up your terminal settings. If you're interested, you can probably try putting the alias definition in various places in your .cshrc, to see what command triggers the problem. (A divide-and-conquer approach means you won't have to perform a huge number of trials.)

我的猜测是,你的一个启动脚本中的某些东西会以一种弄乱你的终端设置的方式与你的postcmd别名进行交互。如果您有兴趣,可以尝试将别名定义放在.cshrc中的不同位置,以查看触发问题的命令。 (分而治之的方法意味着你不必进行大量的试验。)

It would be interesting to see if the problem still exists in later versions of tcsh. It could well be a tcsh bug, but I don't see anything relevant at http://bugs.gw.com/.

在tcsh的更高版本中查看问题是否仍然存在将会很有趣。它可能是一个tcsh错误,但我在http://bugs.gw.com/上看不到任何相关内容。

#2


2  

According to this link, the problem is only existant on Red Hat machines only.

根据此链接,问题仅在Red Hat机器上存在。

From their site:

从他们的网站:

The "less" command

“少”命令

When I try to use the less command, instead of seeing the contents of a file I see messages that I usually see only when I log in. How do I fix this?

当我尝试使用less命令时,不会看到文件的内容,而是看到我通常只在登录时才会看到的消息。如何解决此问题?

If the .cshrc, .profile, or any other shell start-up file in your home directory prints any text, less will display that text instead of your file. To get rid of this behavior, test to see if you can write to standard output before you actually print anything. For example, assume you're using tcsh shell, and you've put the following statements in your .cshrc file:

如果主目录中的.cshrc,.profile或任何其他shell启动文件打印任何文本,则less将显示该文本而不是文件。要摆脱这种行为,请在实际打印任何内容之前测试是否可以写入标准输出。例如,假设您使用的是tcsh shell,并且已将以下语句放在.cshrc文件中:

echo ".cshrc here"
echo "I am logged on to machine $hostname"

Replace this with:

将其替换为:

 # "-t 1" is true only if standard output is enabled;
 # if not, then don't write any messages.
 if ( -t 1 ) then                                              
    echo ".cshrc here"
    echo "I am logged on to machine $hostname"
 endif

An equivalent test in an sh-style shell:

sh样式shell中的等效测试:

 if [ -t 1 ]; then
    echo ".profile here"
    echo "I am logged on to machine $hostname"
 fi

A FAQ within a FAQ: Why does less do this?

常见问题解答中的常见问题解答:为什么较少这样做?

In RedHat Linux, less has a facility to display other types of files in addition to plain text. For example, if you type:

在RedHat Linux中,除了纯文本之外,还有更少的工具来显示其他类型的文件。例如,如果您键入:

less ${NevisAppBase}/src/archive-tar/gcc-2.95.2.tar.gz

you will see a list of the compressed files in gcc-2.95.2.tar.gz, instead of binary garbage. However, to enable this facility, less has to invoke a sub-shell. If that sub-shell writes anything to standard output, you'll see that output instead of your file.

你会看到gcc-2.95.2.tar.gz中的压缩文件列表,而不是二进制垃圾。但是,要启用此功能,请不要再调用子shell。如果该子shell将任何内容写入标准输出,您将看到输出而不是文件。

#1


1  

Based in discussion in the comments, I don't know what's causing the problem, but I've come up with a workaround.

根据评论中的讨论,我不知道是什么导致了这个问题,但我想出了一个解决方法。

I'm unable to reproduce the problem myself, even with tcsh 6.13.00 (which is fairly old, BTW).

我自己无法重现问题,即使使用tcsh 6.13.00(相当陈旧,BTW)。

But creating the alias from .login, or from a file sourced from .login, seems to avoid the problem.

但是从.login或源自.login的文件创建别名似乎可以避免这个问题。

In my own experiments, I aliased postcmd to a command that updates the xterm title bar with a high-resolution timestamp. With the alias defined in my .cshrc, the title bar updates rapidly as my shell starts, indicating that postcmd is executed even while init scripts are being executed, before the first interactive prompt appears. (Aliasing postcmd to something that appends information to a log file could help track this down.)

在我自己的实验中,我将postcmd别名为一个命令,该命令用高分辨率时间戳更新xterm标题栏。使用我的.cshrc中定义的别名,标题栏会在我的shell启动时快速更新,表示即使在执行init脚本时,也会在第一个交互式提示出现之前执行postcmd。 (将postcmd别名化为将信息附加到日志文件的内容可能有助于跟踪此情况。)

Since .login is sourced after .cshrc, moving the alias definition to .login means that fewer commands are executed with the alias in effect. That's not a bad idea anyway; you probably don't need the alias other than in a login shell.

由于.login源自.cshrc,因此将别名定义移动到.login意味着使用别名执行的命令更少。无论如何,这不是一个坏主意;除了在登录shell中,你可能不需要别名。

My guess is that something in one of your startup scripts interacts with your postcmd alias in a way that messes up your terminal settings. If you're interested, you can probably try putting the alias definition in various places in your .cshrc, to see what command triggers the problem. (A divide-and-conquer approach means you won't have to perform a huge number of trials.)

我的猜测是,你的一个启动脚本中的某些东西会以一种弄乱你的终端设置的方式与你的postcmd别名进行交互。如果您有兴趣,可以尝试将别名定义放在.cshrc中的不同位置,以查看触发问题的命令。 (分而治之的方法意味着你不必进行大量的试验。)

It would be interesting to see if the problem still exists in later versions of tcsh. It could well be a tcsh bug, but I don't see anything relevant at http://bugs.gw.com/.

在tcsh的更高版本中查看问题是否仍然存在将会很有趣。它可能是一个tcsh错误,但我在http://bugs.gw.com/上看不到任何相关内容。

#2


2  

According to this link, the problem is only existant on Red Hat machines only.

根据此链接,问题仅在Red Hat机器上存在。

From their site:

从他们的网站:

The "less" command

“少”命令

When I try to use the less command, instead of seeing the contents of a file I see messages that I usually see only when I log in. How do I fix this?

当我尝试使用less命令时,不会看到文件的内容,而是看到我通常只在登录时才会看到的消息。如何解决此问题?

If the .cshrc, .profile, or any other shell start-up file in your home directory prints any text, less will display that text instead of your file. To get rid of this behavior, test to see if you can write to standard output before you actually print anything. For example, assume you're using tcsh shell, and you've put the following statements in your .cshrc file:

如果主目录中的.cshrc,.profile或任何其他shell启动文件打印任何文本,则less将显示该文本而不是文件。要摆脱这种行为,请在实际打印任何内容之前测试是否可以写入标准输出。例如,假设您使用的是tcsh shell,并且已将以下语句放在.cshrc文件中:

echo ".cshrc here"
echo "I am logged on to machine $hostname"

Replace this with:

将其替换为:

 # "-t 1" is true only if standard output is enabled;
 # if not, then don't write any messages.
 if ( -t 1 ) then                                              
    echo ".cshrc here"
    echo "I am logged on to machine $hostname"
 endif

An equivalent test in an sh-style shell:

sh样式shell中的等效测试:

 if [ -t 1 ]; then
    echo ".profile here"
    echo "I am logged on to machine $hostname"
 fi

A FAQ within a FAQ: Why does less do this?

常见问题解答中的常见问题解答:为什么较少这样做?

In RedHat Linux, less has a facility to display other types of files in addition to plain text. For example, if you type:

在RedHat Linux中,除了纯文本之外,还有更少的工具来显示其他类型的文件。例如,如果您键入:

less ${NevisAppBase}/src/archive-tar/gcc-2.95.2.tar.gz

you will see a list of the compressed files in gcc-2.95.2.tar.gz, instead of binary garbage. However, to enable this facility, less has to invoke a sub-shell. If that sub-shell writes anything to standard output, you'll see that output instead of your file.

你会看到gcc-2.95.2.tar.gz中的压缩文件列表,而不是二进制垃圾。但是,要启用此功能,请不要再调用子shell。如果该子shell将任何内容写入标准输出,您将看到输出而不是文件。