zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: zsh-users@zsh.org
Subject: Re: How to (properly) remove the last entry from history with command_not_found_handler
Date: Sun, 20 Jul 2014 18:45:55 +0100	[thread overview]
Message-ID: <20140720184555.5bb66e8c@pws-pc.ntlworld.com> (raw)
In-Reply-To: <53CB860C.8080509@sternenrei.ch>

On Sun, 20 Jul 2014 11:04:12 +0200
Jochen Keil <jrk@sternenrei.ch> 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


  reply	other threads:[~2014-07-20 17:46 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-20  9:04 Jochen Keil
2014-07-20 17:45 ` Peter Stephenson [this message]
2014-07-20 18:13 ` Bart Schaefer
2014-07-20 20:48   ` Jochen Keil
2014-07-21 17:46     ` ZyX

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140720184555.5bb66e8c@pws-pc.ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).