Created
August 12, 2020 23:09
-
-
Save kirbysayshi/aacee54e42c3518a4fdba9a94add0d0b to your computer and use it in GitHub Desktop.
port bash_history to zsh, while preserving existing zsh history too!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, Modified by Drew Petersen | |
*/ | |
// change if you don't use default values | |
const BashHistoryFilePath = process.env.HOME + "/gtdrewp-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 | |
const start = (Date.now() / 1000).toFixed(0) * 1; | |
bashHistFileLines.forEach((command, index) => { | |
const time = start - (bashHistFileLines.length - index); | |
if (command && command.trim().length > 0) | |
zshHistFileLines.push(`: ${time}:0;${command}\n`); | |
}); | |
const combined = fs.readFileSync(ZshHistoryFilePath, 'utf8').split('\n'); | |
combined.push.apply(combined, zshHistFileLines); | |
fs.writeFileSync(ZshHistoryFilePath, combined.join(''), {encoding: 'utf8', flag: 'a+'}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment