Posts Tagged ‘terminal’

Reduce the delay after ESC in ncurses

In ncurses-based programs, the default delay after the ESC key is way too long. (Such a delay is necessary to differentiate the ESC key from function keys.)

There seems to be no such delay in VIM. But with strace, we can find that VIM actually delays, but for only 25 milliseconds, shorter than 100ms – the minimal time interval that human can perceive:

read(0, "\33"..., 4096)                 = 1
select(6, [0 3 5], NULL, [0 3], {0, 0}) = 0 (Timeout)
gettimeofday({1253979790, 537775}, NULL) = 0
select(6, [0 3 5], NULL, [0 3], {0, 25000}) = 0 (Timeout)

If 25ms works reliably for VIM, so should it do for other programs. There seems to be no functions setting this interval in ncurses, but we can directly modify the global variable ESCDELAY. Ncurses also accepts the environment variable also named ESCDELAY, so we may also want to honor the user’s choice:

if (getenv ("ESCDELAY") == NULL)
  ESCDELAY = 25;

Not confident in its portability, I detect this undocumented (it seems) feature with cmake in tiary (my project).


Update (Feb. 22, 2010): It’s preferable to use the function set_escdelay.

Tags: , ,

How BASH Changes Terminal Window Title

In many distributions bash automatically changes the terminal title. I thought it was hardcoded in bash, but it turns out to be not. It is usually implemented with PROMPT_COMMAND.

Environment variable $PROMPT_COMMAND defines the command to be automatically executed before displaying the primary prompt (i.e. $PS1). It is set by a script that is sourced by interative instances of bash. In Gentoo it is in /etc/bash/bashrc:

case ${TERM} in
    xterm*|rxvt*|Eterm|aterm|kterm|gnome*|interix)
        PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\007"'
        ;;
    screen)
        PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/$HOME/~}\033\\"'
        ;;
esac

In fact, the first version above also works with newer versions of screen.

Tags: ,

Jumbled Characters after Catting a Binary File

When this happens, simply press Ctrl-V Ctrl-O Ctrl-M. Or alternatively, type “reset” and Return (Enter).

A terminal interpretes 0x0e byte as “activates the G1 character set”, and 0x0f as “activates the G0 character set”. The characters we read are in the G0 set. So, if there is no byte 0x0f after the last 0x0e in a binary file, everything will be shown in the unreadable G1 set, including the next shell prompt.

How does Ctrl-V Ctrl-O Ctrl-M work?
Ctrl-V is an ‘escape character’ – the next keystroke will always be interpreted as a literal character; Ctrl-O is 0x0f; Ctrl-M is carriage return. So the shell gets the command “x0f” and outputs the error message “bash: x0f: command not found”. The byte 0x0f in this message turns the active character back to the readable G0.

G1 character set is not often used these days. Konsole chooses not to implement it at all, so we never have this problem in Konsole.

Tags: , ,