From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 21421 invoked by alias); 20 Jul 2014 17:46:02 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 18978 Received: (qmail 6585 invoked from network); 20 Jul 2014 17:45:58 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 X-Originating-IP: [86.6.157.246] X-Spam: 0 X-Authority: v=2.1 cv=euRRz+ZX c=1 sm=1 tr=0 a=BvYiZ/UW0Fmn8Wufq9dPrg==:117 a=BvYiZ/UW0Fmn8Wufq9dPrg==:17 a=NLZqzBF-AAAA:8 a=ZqqY5zG1_mUA:10 a=uObrxnre4hsA:10 a=kj9zAlcOel0A:10 a=iGaTfj0NOS8u3rMh17gA:9 a=CjuIK1q_8ugA:10 Date: Sun, 20 Jul 2014 18:45:55 +0100 From: Peter Stephenson To: zsh-users@zsh.org Subject: Re: How to (properly) remove the last entry from history with command_not_found_handler Message-ID: <20140720184555.5bb66e8c@pws-pc.ntlworld.com> In-Reply-To: <53CB860C.8080509@sternenrei.ch> References: <53CB860C.8080509@sternenrei.ch> X-Mailer: Claws Mail 3.8.0 (GTK+ 2.24.7; x86_64-redhat-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 20 Jul 2014 11:04:12 +0200 Jochen Keil wrote: > I'm trying to remove the last entry from my $HISTFILE when the command > was not found. My current attempt involves the > command_not_found_handler() hook: > > function command_not_found_handler() > { > sed -i '$d' $HISTFILE > return 127 > } > > However, I'm not sure if it's ok to manipulate the history file directly > with sed, or if I'm risking corruption. You're can hit problems if you have multiple shells running at once. You'd need to lock the file --- it is possible to do that in a way that's compatibile with how the shell works, either creating a symlink with the name of the history file with .LOCK on the end or, if you have HISTFCNTLLOCK set, using flock, which you can get access to with "zsystem flock". Another approach is to have a zshaddhistory hook --- either a function or zshaddhistory or a function named in the array zshaddhistory_functions --- decide whether the command isn't going to be found. But that's quite a lot of work, too: you need to split off the first word, look it up as an alias, possibly recursively, then decide if the result is a reserved word, an external command, a function, an assignment... all possible but takes some work. I couldn't think of a way to make zshaddhistory and command_not_found_handler together. pws