Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const fs = require('fs');
/**
* This script transfers bash history to zsh history
* Change bash and zsh history files, if you don't use defaults
*
* Usage: node bash_to_zsh_history.js
*
* Author: Matteo Gaggiano
*/
// change if you don't use default values
const BashHistoryFilePath = process.env.HOME + "/.bash_history";
const ZshHistoryFilePath = process.env.HOME + "/.zsh_history";
// Read the bash history file
const bashHistFile = fs.readFileSync(BashHistoryFilePath, 'utf8');
const bashHistFileLines = bashHistFile.split("\n");
// Open the zsh history file
const zshHistFileLines = [];
// Get timestamp required for zsh history file format and update the history file
let time = (Date.now() / 1000).toFixed(0) * 1;
bashHistFileLines.forEach((command, index) => {
time += 1;
if (command && command.trim().length > 0)
zshHistFileLines.push(`: ${time}:0;${command}\n`);
});
fs.writeFileSync(ZshHistoryFilePath, zshHistFileLines.join(''), {encoding: 'utf8', flag: 'a+'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment