Skip to content

Instantly share code, notes, and snippets.

@op
Created September 28, 2012 21:25
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save op/3802158 to your computer and use it in GitHub Desktop.
Save op/3802158 to your computer and use it in GitHub Desktop.
Bash history to Zsh history
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This is how I used it:
# $ cat ~/.bash_history | bash-history-to-zsh-history >> ~/.zsh_history
import sys
def main():
timestamp = None
for line in sys.stdin.readlines():
line = line.rstrip('\n')
if line.startswith('#') and timestamp is None:
t = line[1:]
if t.isdigit():
timestamp = t
continue
elif timestamp:
sys.stdout.write(': %s:0;%s\n' % (timestamp, line))
timestamp = None
if __name__ == '__main__':
main()
@markuz
Copy link

markuz commented Jan 3, 2014

Why don't just use "print" instead of sys.stdout.write?

@nealian
Copy link

nealian commented Nov 16, 2016

A ruby alternative also exists; https://gist.github.com/goyalankit/a1c88bfc69107f93cda1
(Not to rag on this script at all; it's awesome. I just wanted to point to other, also-awesome things.)

@itunc
Copy link

itunc commented Jun 27, 2017

Although I do have Python (2.7) installed, it does not work (does not do anything) with zsh on mac, OSX 10.12.
I solved my problem with the Ruby version, provided by naelian.
Thanks

@fernandoacorreia
Copy link

This didn't work for me (I think my .bash_history file wasn't in the expected format), but this fork did: https://gist.github.com/muendelezaji/c14722ab66b505a49861b8a74e52b274

@epichub
Copy link

epichub commented Dec 20, 2017

If anyone wants a version which converts from csh to zsh history, I made a version here (with link back to this for purpose of origin/acknowledgement): https://gist.github.com/epichub/1cda5e4d2ac50788052f8c2f1448b414

@xibuka
Copy link

xibuka commented Feb 13, 2018

This works for me perfect, thanks.

@krasnobaev
Copy link

Didn't work for me, but this one did: https://gist.github.com/muendelezaji/c14722ab66b505a49861b8a74e52b274
(Ubuntu 16.04.4)

@brnl
Copy link

brnl commented Mar 7, 2019

Just an update that this one didn't work for me on Ubuntu 18.10, Python 2.7.15

The link posted by @krasnobaev worked for me!

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