Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: An example of writing a custom history file?

On Mon, Dec 15, 2014 at 7:26 PM, Bart Schaefer
<schaefer@xxxxxxxxxxxxxxxx> wrote:
> On Dec 15, 12:05pm, Rocky Bernstein wrote:
> }
> } Making the change suggested, adding 1000 doesn't change the behavior - no
> } file is written. Again, here is the entire 12-line program:
> }
> }     #!/usr/bin/zsh
> }     fc -ap /tmp/example_history 1000
> }
> }     local line
> }     # Read lines and add them to history
> }     while vared -h -p "hey: " line
> }     do
> }         [[ $line == 'quit' ]] && exit 0
> }         # The -s option below adds the line to the history
> }         print -s $line
> }         line=''
> }     done
> This worked for me exactly as written; I got lines saved in the
> /tmp/example_history file.  However, I was working with an interactive
> shell, which normally saves history at exit.  If you are trying to run
> this as a stand-alone script, you probably don't want "exit 0" there.
> More likely you just want to "break" and let the loop finish.  Also
> if running standalone, there's no "function scope" so I suspect that
> foils the usual action of "fc -a".

I tried the script with and without -a and a -P at the end, and
nesting the body inside a () { } anon function, nothing "helps" in the
case of a non-interactive shell, and with an interactive shell, none
of it is needed.

I found only one place in the manpage that mentions history only being
active in an interactive shell, perhaps it should be clearer about it?
I also noticed that explicit fc -l and fc -W etc do nothing but also
don't print a warning when invoked non-interactively.

  The file to save the history in when an interactive shell
  exits. If unset, the history is not saved.

I think before we added print -s, there was no way to get history
non-interactively (I suppose fc -R?) and likely nobody thought to
mention the restriction in the fc docs when adding it?

Mikael Magnusson

Messages sorted by: Reverse Date, Date, Thread, Author