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

Re: Editing history stack during runtime?



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
    }
  }



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