zsh-users
 help / color / mirror / code / Atom feed
* Editing history stack during runtime?
@ 2008-02-29  1:09 Richard Hartmann
  2008-02-29 16:43 ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Richard Hartmann @ 2008-02-29  1:09 UTC (permalink / raw)
  To: zsh users

Hi all,

is there a way to stuff all my local history (i.e. from from hist files)
into $EDITOR, edit it and put back whatever I save to it?


Thanks,
Richard


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Editing history stack during runtime?
  2008-02-29  1:09 Editing history stack during runtime? Richard Hartmann
@ 2008-02-29 16:43 ` Bart Schaefer
  2008-02-29 19:42   ` Richard Hartmann
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2008-02-29 16:43 UTC (permalink / raw)
  To: zsh users

On Feb 29,  2:09am, Richard Hartmann wrote:
}
} is there a way to stuff all my local history (i.e. from from hist files)
} into $EDITOR, edit it and put back whatever I save to it?

There are a couple of different things you might mean by this, so I'm
not sure how to answer.  Does "put back" mean "keep in $HISTFILE" (for
the next shell that starts) or does it mean "load immediately"?  And
does "local history" mean "most current history of this shell" or does
it simply mean whatever is in $HISTFILE at the moment?

If you don't have INC_APPEND_HISTORY set and you want the most recent
history of the current shell, you need to write the file first with
"fc -W".  That might get into complications if SAVEHIST and HISTSIZE
have different values.

Then you can simply run "$EDITOR $HISTFILE", and finally re-read it
with "fc -R".  However, if you have SHARE_HISTORY set, then editing
the file in place may not be a good idea, so there's no way to give
you a direct answer without knowing what end result you want.

Then there are the added complications of EXTENDED_HISTORY, which puts
things in $HISTFILE you have to be careful about editing (it's still
all just text, though).  You can arrange to strip those so that you
can edit without breaking anything, but then when you rewrite the file
you'll lose all that information.

So ... if this doesn't tell you enough to get where you want to be,
you're going to have to ask a more specific question.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Editing history stack during runtime?
  2008-02-29 16:43 ` Bart Schaefer
@ 2008-02-29 19:42   ` Richard Hartmann
  2008-03-01  3:22     ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Richard Hartmann @ 2008-02-29 19:42 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh users

On Fri, Feb 29, 2008 at 5:43 PM, Bart Schaefer
<schaefer@brasslantern.com> wrote:


>  There are a couple of different things you might mean by this, so I'm
>  not sure how to answer.

I want to take the session history, i.e. what was typed since last login,
and edit that. Then, I want whatever I save to replace the session
history.

Histories of other shells or in files should not be affected in any way.


>  Then there are the added complications of EXTENDED_HISTORY, which puts
>  things in $HISTFILE you have to be careful about editing (it's still
>  all just text, though).  You can arrange to strip those so that you
>  can edit without breaking anything, but then when you rewrite the file
>  you'll lose all that information.

It is probably easiest to just put it into $EDITOR with the commands.
I am using EXTENDED_HISTORY, but I don't mind the information
in my Vim. If all else fails, blockwise select & remove :)


Thanks,
Richard


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Editing history stack during runtime?
  2008-02-29 19:42   ` Richard Hartmann
@ 2008-03-01  3:22     ` Bart Schaefer
  2008-03-01 10:38       ` Richard Hartmann
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2008-03-01  3:22 UTC (permalink / raw)
  To: zsh users

On Feb 29,  8:42pm, Richard Hartmann wrote:
} 
} I want to take the session history, i.e. what was typed since last login,
} and edit that. Then, I want whatever I save to replace the session
} history.

I have a vague recollection of something like this having been posted
before, but not since we added "always" blocks and similar fun, so:

  edit_hist () {
    emulate -LR zsh
    unsetopt append_history inc_append_history share_history

    # You may want these in this case even if not otherwise:
    # setopt hist_expire_dups_first hist_ignore_all_dups

    # The following gibberish creates a unique tempfile
    # and makes sure that we'll write the whole history
    local SAVEHIST=$HISTSIZE HISTFILE=${:-=(:)}$$

    # Make sure we write a new file and rename to the tempfile
    setopt no_hist_save_by_copy

    # Finally, the actual useful work
    fc -W
    {
      if ${VISUAL:-${EDITOR:-vi}} $HISTFILE
      then
        # Truncate internal history to discard deleted lines
        HISTSIZE=0
        HISTSIZE=$SAVEHIST
        fc -R
      fi
    } always {
      rm -f $HISTFILE
    }
  }


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Editing history stack during runtime?
  2008-03-01  3:22     ` Bart Schaefer
@ 2008-03-01 10:38       ` Richard Hartmann
  2008-03-01 12:59         ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Richard Hartmann @ 2008-03-01 10:38 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh users

On Sat, Mar 1, 2008 at 4:22 AM, Bart Schaefer <schaefer@brasslantern.com> wrote:


>  I have a vague recollection of something like this having been posted
>  before, but not since we added "always" blocks and similar fun, so:

Wow, thanks. I fear we misunderstood each other in one regard, though:
I do not want to have the contents of the history file in there, only the
history accumulated since I logged in to this one shell. I will probably
keep the above one as well, though. Both can be very useful.


Richard


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Editing history stack during runtime?
  2008-03-01 10:38       ` Richard Hartmann
@ 2008-03-01 12:59         ` Bart Schaefer
  2008-03-03 12:25           ` Richard Hartmann
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2008-03-01 12:59 UTC (permalink / raw)
  To: zsh users

On Mar 1, 11:38am, Richard Hartmann wrote:
} Subject: Re: Editing history stack during runtime?
}
} Wow, thanks. I fear we misunderstood each other in one regard, though:
} I do not want to have the contents of the history file in there

The contents of the history file are not in there, except that I think
you misunderstand something:

} only the history accumulated since I logged in to this one shell.

"The history accumulated since I logged in" *includes* the history
read from the history file at shell startup.  There's no way to pick
out only what happened after that; it's all in the same stack, with
the oldest stuff falling off one end and the latest stuff added to
the other.

Anything in the history file but old enough to have fallen out of the
shell history is not included in the file that gets edited.  Anything
in the overlap will be included, but only because it's still in the
current history.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Editing history stack during runtime?
  2008-03-01 12:59         ` Bart Schaefer
@ 2008-03-03 12:25           ` Richard Hartmann
  2008-03-03 16:30             ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Richard Hartmann @ 2008-03-03 12:25 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh users

On Sat, Mar 1, 2008 at 1:59 PM, Bart Schaefer <schaefer@brasslantern.com> wrote:


>  "The history accumulated since I logged in" *includes* the history
>  read from the history file at shell startup.  There's no way to pick
>  out only what happened after that; it's all in the same stack, with
>  the oldest stuff falling off one end and the latest stuff added to
>  the other.

Hmm, now that I think about it, this _does_ make sense. Matter of fact,
nothing else would. I will probably try and hack something together that
uses a custom variable to store invocation time of the shell and then
parses out everything on the stack that is older than this time.


Thanks :)
Richard


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Editing history stack during runtime?
  2008-03-03 12:25           ` Richard Hartmann
@ 2008-03-03 16:30             ` Bart Schaefer
  0 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2008-03-03 16:30 UTC (permalink / raw)
  To: zsh users

On Mar 3,  1:25pm, Richard Hartmann wrote:
}
} On Sat, Mar 1, 2008 at 1:59 PM, Bart Schaefer <schaefer@brasslantern.com> wrote:
} >  "The history accumulated since I logged in" *includes* the history
} >  read from the history file at shell startup.
} 
} Hmm, now that I think about it, this _does_ make sense. Matter of fact,
} nothing else would. I will probably try and hack something together that
} uses a custom variable to store invocation time of the shell

Hmm.  Actually you can get away with discarding all history entries with
history number <= than the original value of SAVEHIST (from *before* the
assignment SAVEHIST=$HISTSIZE in the edit_history function).  Assuming
that you haven't changed SAVEHIST since the last time the history file
was written, of course -- if you change SAVEHIST a lot you'll have to do
more work to get the startup-time maximum history number.

It's not particularly easy to discard those entries, though.  Probably
the best way is to put the "fc -W" in a subshell and truncate the history
there, like so:

    (
      # Needs recent-enough zsh to have HISTCMD
      if ((HISTCMD > histcmd_at_startup))
      then HISTSIZE=$((HISTCMD - histcmd_at_startup))
      fi
      fc -W
    )


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2008-03-03 16:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-29  1:09 Editing history stack during runtime? Richard Hartmann
2008-02-29 16:43 ` Bart Schaefer
2008-02-29 19:42   ` Richard Hartmann
2008-03-01  3:22     ` Bart Schaefer
2008-03-01 10:38       ` Richard Hartmann
2008-03-01 12:59         ` Bart Schaefer
2008-03-03 12:25           ` Richard Hartmann
2008-03-03 16:30             ` Bart Schaefer

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