Skip to content

Instantly share code, notes, and snippets.

@espinuevajaime
Forked from dvdbng/zsh_to_fish.py
Created February 11, 2019 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espinuevajaime/1da0f2e30b8657dd70cffaebb243c4af to your computer and use it in GitHub Desktop.
Save espinuevajaime/1da0f2e30b8657dd70cffaebb243c4af to your computer and use it in GitHub Desktop.
Migrate zsh history to fish
import os
import re
def zsh_to_fish(cmd):
return (cmd.replace('&&', '; and ')
.replace('||', '; or '))
def is_valid_fish(cmd):
for reg in r'^\S+=', r'\$\(', r'\[ ', r'`', r'\\$':
if re.match(reg, cmd):
return False
return True
with open(os.path.expanduser('~/.local/share/fish/fish_history'), 'a') as o:
with open(os.path.expanduser('~/.zsh_history')) as f:
for line in f:
line = line.strip()
if line and re.match('^:\s+\d+:\d;', line):
meta, command = line.split(';', 1)
command = zsh_to_fish(command)
if is_valid_fish(command):
time = meta.split(':')[1].strip()
print('Add', command)
o.write('- cmd: %s\n when: %s\n' % (command, time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment