-
Star
(188)
You must be signed in to star a gist -
Fork
(36)
You must be signed in to fork a gist
-
-
Save muendelezaji/c14722ab66b505a49861b8a74e52b274 to your computer and use it in GitHub Desktop.
| #!/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() |
nice, worked like a charm on my new MacBook Pro. 👍 thank you
Perfect. Thanks!
thanks, worked on a new M1 MBP
Thanks, worked on Catalina with python 2.7.16
To get it to work with hstr, I had to use int(time.time())
Thanks for saving my history!!
On WSL had pretty big history and was using HSTR, so after importing the history had 2 problems:
- Error
zsh: corrupt history file /home/user/.zsh_history - HSTR showing timestamps
So the solution for me was modified script with thanks to @Zackhardtoname and then several additional steps: - Write date to some other file
.zsh_history_tmp strings .zsh_history_tmp > .zsh_historyfc -R .zsh_history
Works fine in Ubuntu 22.04, thanks for it!
Works fine in Linux Mint Debian Edition 5 (LMDE 5). Thank you.
Works fine on RockyLinux 9 (Workstation) with zsh 5.8, ohmyzsh and python3.9.10.
Many thx. Great timesaver!
Merci!
if zsh_history format is
: 1670471184:0;cat ~/.zsh_history
need use int(time.time()) at line 21
Thanks!
Thanks
gracias!!!
Worked for me, thanks.
Thanks!!! Helped a lot!
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. 🤔
Thank you!!
Thanks!
Worked perfectly. Thanks
If you have UnicodeEncodeError:
lines = []
with open("/home/mmatrosov/.bash_history", "rb") as f:
while bytes := f.readline():
lines.append(bytes.decode("ascii", "backslashreplace"))
hero of my day
thanks!
Updating the code to add bash history before zsh history.
I also code a function to joint bash command compound with \n (enter), like bash loops or ctrl+v command.
from sys import stdin, stdout
TIMESTAMP_FIRST_ZSH_CMD = 1737556882
def treat_enter(lines):
fixed_lines = []
full_cmd = ""
count = 0
while count < len(lines):
full_cmd += lines[count].strip()
if count == len(lines) - 1 or not lines[count+1].startswith(" ") or not lines[count].endswith("\n"):
fixed_lines.append(full_cmd)
full_cmd = ""
count += 1
return fixed_lines
def main():
lines = stdin.readlines()
# print(f"Before Treat: {len(lines)} lines")
lines = treat_enter(lines)
# print(f"After Treat: {len(lines)} lines")
count_tt = TIMESTAMP_FIRST_ZSH_CMD - len(lines)
for line in lines:
line = line.rstrip('\n')
stdout.write(": %s:0;%s\n" % (count_tt, line))
count_tt += 1
if __name__ == "__main__":
main()To use this command:
Update the
TIMESTAMP_FIRST_ZSH_CMDcode variable with timestamp of the oldest zsh command.
cat ~/.bash_history | python3 code.py > ~/.zsh_history_new && cat ~/.zsh_history >> ~/.zsh_history_newThe file ~/.zsh_history_new will be created.
Working on Debian 11 (stock) python, either run the script as:
Or, symlink python3 > python: