sharing or synchronizing history between Zsh and Bash

7,893

Solution 1

If you are using the defaults for bash and zsh:

$ cat ~/.histfile >> ~/.bash_history
$ youreditor ~/.zshrc
# Here change your config to:
HISTFILE=~/.bash_history
$ rm ~/.histfile

Now you have the same file for history in both shells.

Solution 2

In response to Elad, people may have .bash_history files that have an extra line before each command that starts with (#) and has trailing digits following (123456789), for example: #123456789. If your bash_history file has these extra lines, use this modified version of Elad's code to process a clean zsh formatted history to use. Thanks Elad for the quick conversion code.

/*
 * You should backup your .bash_history file first doing this:
 * $ cp ~/.bash_history ~/.bash_history.backup
 * 
 * create the .js file to use first:
 * $ touch ~/.bash-history-to-zsh-history.js
 *
 * This is how I use it based on Elads example:
 * $ node ~/.bash-history-to-zsh-history.js >> ~/.zsh_history
 *
 **/

var fs = require("fs");
var a = fs.readFileSync(".bash_history");
var time = Date.now();
a.toString().split("\n").forEach(function(line){
  if (line.indexOf("#")!=0) console.log(": "+ (time++) + ":0;"+line);
});

Solution 3

Not exactly what you were looking for, but in order to import from bash to zsh, you can use this node.js script:

// This is how I used it:
// $ node bash-history-to-zsh-history.js >> ~/.zsh_history

var fs = require("fs");
var a = fs.readFileSync(".bash_history");
var time = Date.now();
a.toString().split("\n").forEach(function(line){
  console.log(": "+ (time++) + ":0;"+line);
});

Source

Share:
7,893

Related videos on Youtube

Andrei
Author by

Andrei

Updated on September 18, 2022

Comments

  • Andrei
    Andrei over 1 year

    I often find myself switching between Bash and Zsh, and using the history search functionality to recover a command.

    However, since Bash and Zsh have different history files, I often find that the command I'm searching for has been executed in the other shell.

    Is there any way to share or synchronize history between the two?

  • Neil Traft
    Neil Traft almost 10 years
    Does this really work?? The two histfiles have completely different formats!
  • Rufo El Magufo
    Rufo El Magufo almost 10 years
    Yes, both shells use the same format. One command per line.
  • Neil Traft
    Neil Traft almost 10 years
    My .zsh_history has lines like : 1399608924:0;hg diff whereas my .bash_history has simply hg diff. Maybe it's because I'm using oh-my-zsh?
  • Rufo El Magufo
    Rufo El Magufo almost 10 years
    Maybe. Or some option of zsh. I don't know.
  • Chris
    Chris about 7 years
    zsh history for me is stored in ~/.zsh_history
  • Admin
    Admin almost 2 years
    I added this configuration into the file .zshrcsetopt appendhistory autocd extendedglob nomatch and then copy the content of .bash_history into .zsh_history, old and new format is working like (: 1399608924:0;hg diff)