has_many :codes
shellscriptingbashzshmacOS

Easily sync your shell history between computers

Published  

For work, I often flip between my M2 Pro mini and an Intel MBP. To keep my document, code, and other data in sync, I use Nextcloud. It's a cool open-source tool, kind of like Dropbox or Google Drive, but with some extra bells and whistles.

I use the terminal a ton, so one thing that bugged me was syncing my zsh shell history. Just syncing the `~/.zsh_history` file with Nextcloud didn't cut it. One computer's history would just overwrite the other's, causing a mess - i.e. loss of commands and/or sync conflicts. Also, I wanted to merge all the commands from both computers and remove duplicates, so I'd have the same shell history everywhere.

I tried a bunch of different solutions, both free and paid, but none of them hit the mark. They all had their own problems. Atuin looked promising, but it didn't always sync correctly. 

A lot of the solutions I found were way too complicated for such a simple problem. After a bit of tinkering, I came up with a simple way to sync my shell history that actually works! The only requirement is a private git repository on Github (or elsewhere as long as it's accessible from multiple machines). The shell history is automatically synced using this repository and is stored in an encrypted format just in case. Syncing happens in background as you use the terminal and doesn't disrupt or slow down your commands at all.

It also uses fzf so that Ctrl-r and the up arrow key, used to find a previous command from the history that you want to run again, show a "fuzzy search" window that makes it quicker to find the command you are looking for. 

Check it out on my GitHub repo.

© Vito Botta