zsh-users
 help / color / mirror / code / Atom feed
* Saving commands from a session
@ 2015-01-12 17:01 Vin Shelton
  2015-01-12 17:19 ` Mikael Magnusson
  0 siblings, 1 reply; 3+ messages in thread
From: Vin Shelton @ 2015-01-12 17:01 UTC (permalink / raw)
  To: zsh-users

In addition to saving commands in .zhistory, I like to save commands
by session, so I can save and later search the sequence of commands I
executed at a particular time.  In order to do this, in .zlogout I
compared all commands in history against the date and time of the
first command saved:

# Get the date and time of the first command in the shell history.
fc -lin -$HISTCMD -$((HISTCMD-1)) | read d t cmd

# Ignore all commands that have the same date and time.
# They were read in when the shell started.
fc -lin -$HISTCMD | grep -v "^$d $t" >> $outfile

I was never terribly impressed by the elegance of this solution, but
it worked, more or less (I believe it could drop the first command or
two I entered if they happened to have the same date and time as the
start of the shell).

However, I recently started using

setopt EXTENDED_HISTORY

and this has the effect of keeping the original date and time the
command was executed in .zhistory, so when the historical commands are
read into the shell, they no longer all have the same date and time.

Is there a more elegant way to save only those commands that have been
executed in this instance of the shell?

Thanks in advance,
  Vin Shelton


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

* Re: Saving commands from a session
  2015-01-12 17:01 Saving commands from a session Vin Shelton
@ 2015-01-12 17:19 ` Mikael Magnusson
  2015-01-20 19:33   ` Vin Shelton
  0 siblings, 1 reply; 3+ messages in thread
From: Mikael Magnusson @ 2015-01-12 17:19 UTC (permalink / raw)
  To: Vin Shelton; +Cc: zsh-users

On Mon, Jan 12, 2015 at 6:01 PM, Vin Shelton <acs@alumni.princeton.edu> wrote:
> In addition to saving commands in .zhistory, I like to save commands
> by session, so I can save and later search the sequence of commands I
> executed at a particular time.  In order to do this, in .zlogout I
> compared all commands in history against the date and time of the
> first command saved:
>
> # Get the date and time of the first command in the shell history.
> fc -lin -$HISTCMD -$((HISTCMD-1)) | read d t cmd
>
> # Ignore all commands that have the same date and time.
> # They were read in when the shell started.
> fc -lin -$HISTCMD | grep -v "^$d $t" >> $outfile
>
> I was never terribly impressed by the elegance of this solution, but
> it worked, more or less (I believe it could drop the first command or
> two I entered if they happened to have the same date and time as the
> start of the shell).
>
> However, I recently started using
>
> setopt EXTENDED_HISTORY
>
> and this has the effect of keeping the original date and time the
> command was executed in .zhistory, so when the historical commands are
> read into the shell, they no longer all have the same date and time.
>
> Is there a more elegant way to save only those commands that have been
> executed in this instance of the shell?
>
> Thanks in advance,
>   Vin Shelton

Maybe you want to write out each entry incrementally from
zshaddhistory() instead? Zsh does track which history entries are
"local" and which are imported from history, but I don't believe you
can directly access this information.

-- 
Mikael Magnusson


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

* Re: Saving commands from a session
  2015-01-12 17:19 ` Mikael Magnusson
@ 2015-01-20 19:33   ` Vin Shelton
  0 siblings, 0 replies; 3+ messages in thread
From: Vin Shelton @ 2015-01-20 19:33 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: zsh-users

On Mon, Jan 12, 2015 at 12:19 PM, Mikael Magnusson <mikachu@gmail.com> wrote:
> On Mon, Jan 12, 2015 at 6:01 PM, Vin Shelton <acs@alumni.princeton.edu> wrote:
>> In addition to saving commands in .zhistory, I like to save commands
>> by session, so I can save and later search the sequence of commands I
>> executed at a particular time.  In order to do this, in .zlogout I
>> compared all commands in history against the date and time of the
>> first command saved:
>>
>> # Get the date and time of the first command in the shell history.
>> fc -lin -$HISTCMD -$((HISTCMD-1)) | read d t cmd
>>
>> # Ignore all commands that have the same date and time.
>> # They were read in when the shell started.
>> fc -lin -$HISTCMD | grep -v "^$d $t" >> $outfile
>>
>> I was never terribly impressed by the elegance of this solution, but
>> it worked, more or less (I believe it could drop the first command or
>> two I entered if they happened to have the same date and time as the
>> start of the shell).
>>
>> However, I recently started using
>>
>> setopt EXTENDED_HISTORY
>>
>> and this has the effect of keeping the original date and time the
>> command was executed in .zhistory, so when the historical commands are
>> read into the shell, they no longer all have the same date and time.
>>
>> Is there a more elegant way to save only those commands that have been
>> executed in this instance of the shell?
>
> Maybe you want to write out each entry incrementally from
> zshaddhistory() instead? Zsh does track which history entries are
> "local" and which are imported from history, but I don't believe you
> can directly access this information.

Thanks, Mikael.  I followed your advice and am now using zshaddhistory().

Regards,
  Vin


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

end of thread, other threads:[~2015-01-20 19:33 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-12 17:01 Saving commands from a session Vin Shelton
2015-01-12 17:19 ` Mikael Magnusson
2015-01-20 19:33   ` Vin Shelton

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