Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Import bash history to zsh history.
#################################################################
# = This script transfers bash history to zsh history
# = Change bash and zsh history files, if you don't use defaults
#
# = Usage: ruby bash_to_zsh_history.rb
#
# = Author: Ankit Goyal
#################################################################
# change if you don't use default values
BASH_HISTORY_FILE_PATH="#{ENV['HOME']}/.bash_history"
ZSH_HISTORY_FILE_PATH="#{ENV['HOME']}/.zsh_history"
# Read the bash history file
bash_hist_file = File.read(BASH_HISTORY_FILE_PATH)
# Get the list of commands from bash history hile
command_list = bash_hist_file.split("\n")
# Open the zsh history file
zsh_hist_file = File.open(ZSH_HISTORY_FILE_PATH, "a")
# Get timestamp required for zsh history file format and update the history file
time = Time.now.to_i
command_list.each do |command|
time += 1
zsh_hist_file.write(": #{time}:0;#{command}\n")
end
# Close the file
zsh_hist_file.close

Thx the script is fast and good

Worked phenomenally, thank you!

AmbreB commented Dec 1, 2016

Just what I was looking for ! Thank you !!

Thanks !

osahner commented Mar 16, 2017

Perfect!

This command seems to ignore the timestamps in the ~/.bash_history file?

victorhcm commented Jun 26, 2017

I'm getting:

zsh: corrupt history file /home/user/.zsh_history

Did anyone else have this issue?

My .bash_history has 23k lines, I think this might be an issue. I tried to increase ZSH history limit and rerun the converter, but still didn't work.

EDIT: Solution:

mv .zsh_history .zsh_history_bad
strings .zsh_history_bad > .zsh_history
fc -R .zsh_history

I tried this, but I think it was before I increased ZSH history limit, so it was mangling my history to 10k lines. From my .zshrc:

HISTSIZE=1000000  # how many lines of history to keep in memory
SAVEHIST=1000000  # how many lines to keep in the history file

Works fine and fast! Thank you!

leggiero commented Aug 9, 2017

Awesome script! Worked like a charm. Thank you for sharing!

Thanks! 🥇

Thank you! For anyone else who uses this, if you get utf-8 errors, try upgrading ruby

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment