From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19418 invoked from network); 19 Jan 2005 14:24:03 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 19 Jan 2005 14:24:03 -0000 Received: (qmail 93777 invoked from network); 19 Jan 2005 14:23:57 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 19 Jan 2005 14:23:57 -0000 Received: (qmail 2544 invoked by alias); 19 Jan 2005 14:23:25 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 8398 Received: (qmail 2530 invoked from network); 19 Jan 2005 14:23:25 -0000 Received: from unknown (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 19 Jan 2005 14:23:25 -0000 Received: (qmail 92229 invoked from network); 19 Jan 2005 14:22:49 -0000 Received: from mailhost1.csr.com (HELO MAILSWEEPER01.csr.com) (81.105.217.43) by a.mx.sunsite.dk with SMTP; 19 Jan 2005 14:22:43 -0000 Received: from exchange03.csr.com (unverified [10.100.137.60]) by MAILSWEEPER01.csr.com (Content Technologies SMTPRS 4.3.12) with ESMTP id for ; Wed, 19 Jan 2005 14:21:21 +0000 Received: from news01.csr.com ([10.103.143.38]) by exchange03.csr.com with Microsoft SMTPSVC(5.0.2195.6713); Wed, 19 Jan 2005 14:25:14 +0000 Received: from news01.csr.com (localhost.localdomain [127.0.0.1]) by news01.csr.com (8.13.1/8.12.11) with ESMTP id j0JEMg8L020499 for ; Wed, 19 Jan 2005 14:22:42 GMT Received: from csr.com (pws@localhost) by news01.csr.com (8.13.1/8.13.1/Submit) with ESMTP id j0JEMfxA020496 for ; Wed, 19 Jan 2005 14:22:42 GMT Message-Id: <200501191422.j0JEMfxA020496@news01.csr.com> X-Authentication-Warning: news01.csr.com: pws owned process doing -bs To: zsh-users@sunsite.dk Subject: Re: The old backspace/delete problem In-reply-to: <87ekghftq7.fsf@eddy.tactio.lan> References: <87ekghftq7.fsf@eddy.tactio.lan> Date: Wed, 19 Jan 2005 14:22:41 +0000 From: Peter Stephenson X-OriginalArrivalTime: 19 Jan 2005 14:25:14.0636 (UTC) FILETIME=[B11ACCC0:01C4FE32] X-Spam-Checker-Version: SpamAssassin 3.0.2 on a.mx.sunsite.dk X-Spam-Level: X-Spam-Status: No, score=-1.7 required=6.0 tests=AWL,BAYES_00,URIBL_SBL autolearn=no version=3.0.2 X-Spam-Hits: -1.7 Boyd Adamson wrote: > Someone recently mentioned to me a solution to the old "backspace key > sends ^? or ^H" problem that I hadn't heard of, and I thought we here > in zsh land might be able to improve on it. > > The solution I saw was for a ksh user: > > alias ^?='stty erase ^?' > alias ^H='stty erase ^H' > > The idea is that on login, the user types . This > will do one of two things: > 1. The character sent by the backspace key will match the current tty > setting. In this case nothing happens except for a new prompt, or, > 2. The character sent by the backspace key does not match the current > tty setting, the alias is expanded and the tty setting is changed > to match the terminal emulator. > > Now this is nice, but it doesn't help those of use who use a superior > shell, since both ^? and ^H are normally mapped to the same function > (something like backward-delete-char). This is nice in the shell, but > potentially leaves us with a broken backspace key in other command > line programs. > > Any ideas on how we could do this sort of auto-detection in zsh? It's doable; there's a slight catch, but I think I've managed to make it almost invisible to the user. Here's the code first: backward-delete-char-detect() { if (( #KEYS == ##\C-? )); then zmodload -i zsh/sched sched +00:00 "stty erase '^?'" elif (( #KEYS == ##\C-h )); then zmodload -i zsh/sched sched +00:00 "stty erase '^h'" fi zle -A .$WIDGET $WIDGET zle .$WIDGET } zle -N backward-delete-char backward-delete-char-detect zle -N vi-backward-delete-char backward-delete-char-detect This code will run the function backward-delete-char-detect the first time you type a key bound to backward-delete-char. If that's either ^? or ^h, it will record the fact. Then it will restore the original version of backward-delete-char and perform the normal operation. The unpleasantness is that we can't run stty inside the editor widget, since the terminal setup is explicitly restored on exit from zle and any change is lost. So instead we use "sched" to schedule the stty to run immediately, which in practice is the next time the command line is executed. This does seem to work, but the effect consequently isn't instantaneous; the first command that runs immediately after you've deleted a character doesn't yet have the character remapped, because the "sched" event is only examined when control returns to the shell after that command. Alternatively, you could split the stty into the preexec function, but then it becomes messier. -- Peter Stephenson Software Engineer CSR PLC, Churchill House, Cambridge Business Park, Cowley Road Cambridge, CB4 0WZ, UK Tel: +44 (0)1223 692070 ********************************************************************** This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses. www.mimesweeper.com **********************************************************************