Question

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.

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

alias 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)

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:

>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)

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.

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

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.

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?

Was it helpful?

Solution

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).

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

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.)

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.

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.)

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/.

OTHER TIPS

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

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?

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:

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:

 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:

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top