Skip to content

Instantly share code, notes, and snippets.

@dantonnoriega
Last active October 26, 2022 05:45
Show Gist options
  • Save dantonnoriega/2f4d6d5f54cc68236708d2f1b9adc45a to your computer and use it in GitHub Desktop.
Save dantonnoriega/2f4d6d5f54cc68236708d2f1b9adc45a to your computer and use it in GitHub Desktop.
how to append old zsh_history files into a single one, keeping things in order, and updating $HISTFILE

Helpful Resources

WARNING: using fc -p will replace your current history file with a blank one. Tread carefully.

GOAL: merge (or concat or join or append) a set of old zsh_history files into a single one.

Imagine you have 3 zsh history files, with timestamps from oldest to newest: zsh_history0, zsh_history1, zsh_history2.

The approach here is to

  1. Append zsh_history1 to zsh_history0
  2. Append zsh_history2 to zsh_history0 (which will now also contain zsh_history1 data)
  3. Replace ~/.zsh_history with combined zsh_history0

I'll note that I don't actually know what I'm doing. The following two approaches appeared to work?

OPTION 1

# first, make a copy of zsh_history0
cp zsh_history0 zsh_history_merged

# load in zsh_history1 then append it to zsh_history_merged
fc -p zsh_history1
fc -A zsh_history_merged

# load in zsh_history2 then append it to zsh_history_merged
fc -p zsh_history2
fc -A zsh_history_merged

# replace existing history file ($HISTFILE)
cp zsh_history_merged $HISTFILE

OPTION 2

# load in zsh_history0
fc -p zsh_history0

# read in additional files
fc -I -R zsh_history1
fc -I -R zsh_history2

# write the files to zsh_history_merged
fc -W zsh_history_merged

# replace existing history file ($HISTFILE)
cp zsh_history_merged $HISTFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment