Skip to content

Instantly share code, notes, and snippets.

@muendelezaji
Forked from op/bash-history-to-zsh-history.py
Created October 5, 2016 14:18
Show Gist options
  • Save muendelezaji/c14722ab66b505a49861b8a74e52b274 to your computer and use it in GitHub Desktop.
Save muendelezaji/c14722ab66b505a49861b8a74e52b274 to your computer and use it in GitHub Desktop.
Convert Bash history to Zsh history
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This is how I used it:
# $ cat ~/.bash_history | python bash-to-zsh-hist.py >> ~/.zsh_history
import sys
import time
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
else:
sys.stdout.write(': %s:0;%s\n' % (timestamp or time.time(), line))
timestamp = None
if __name__ == '__main__':
main()
@omkardeekonda
Copy link

Worked like a charm !! Thank you 👍
Wondering why zsh framework doesn't have a feature to import the bash history as part of initial setup. 🤔

@carlosmuller
Copy link

Thank you!!

@desmond1121
Copy link

Thanks!

@kevin-vm
Copy link

Worked perfectly. Thanks

@mmatrosov
Copy link

If you have UnicodeEncodeError:

    lines = []
    with open("/home/mmatrosov/.bash_history", "rb") as f:
        while bytes := f.readline():
            lines.append(bytes.decode("ascii", "backslashreplace"))

@kaiwu-astro
Copy link

hero of my day

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