Skip to content

Instantly share code, notes, and snippets.

@firefoxic
Last active January 26, 2026 17:04
Show Gist options
  • Select an option

  • Save firefoxic/e29251b01238a2ef91f24e2bdc8185c2 to your computer and use it in GitHub Desktop.

Select an option

Save firefoxic/e29251b01238a2ef91f24e2bdc8185c2 to your computer and use it in GitHub Desktop.
Zsh to Fish command history converter
#!/usr/bin/env fish
#
# Zsh to Fish command history converter
#
# Usage:
# ./convert_zsh_to_fish_history.fish [zsh_history] [fish_history]
#
set -l zsh_file (count $argv -gt 0; and echo $argv[1]; or echo ~/.zsh_history)
set -l fish_file (count $argv -gt 1; and echo $argv[2]; or echo ~/.local/share/fish/fish_history)
# Check the source file's existence
if not test -f $zsh_file
echo "Error: Zsh history file not found: $zsh_file" >&2
exit 1
end
# Create a directory for Fish history if necessary
set -l fish_dir (dirname $fish_file)
if not test -d $fish_dir
mkdir -p $fish_dir
end
# Backup copy of existing Fish history
if test -f $fish_file
cp $fish_file $fish_file.backup
echo "Backup created: $fish_file.backup"
end
set -l count 0
set -l temp_file (mktemp)
# Read the Zsh history line by line
while read -l line
# Skip empty lines
test -z "$line"; and continue
# Parse the Zsh format: ": timestamp:duration;command"
if string match -qr '^: (\d+):\d+;(.*)$' -- $line
set -l timestamp (string replace -r '^: (\d+):\d+;.*$' '$1' -- $line)
set -l cmd (string replace -r '^: \d+:\d+;(.*)$' '$1' -- $line)
# Escape special characters for YAML
# Fish uses the YAML format, so correct escaping is required
set -l escaped_cmd (string replace -a '\\' '\\\\' -- $cmd)
set escaped_cmd (string replace -a \n '\\n' -- $escaped_cmd)
# Write in Fish format
echo "- cmd: $escaped_cmd" >> $temp_file
echo " when: $timestamp" >> $temp_file
set count (math $count + 1)
end
end < $zsh_file
# Move the temporary file to the target location
mv $temp_file $fish_file
echo "Successfully converted $count commands from $zsh_file to $fish_file"
# Optional: merge with existing history
if test -f $fish_file.backup
echo ""
echo "To merge with existing history, run:"
echo " cat $fish_file.backup $fish_file | sort -u > $fish_file.merged"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment