Use case: distribute zsh's history between machines in source control.
Is that something that I can achieve with zsh's history -- put history in version control the sync between machines? This is to avoid re-typing commands and quick trace.
Use case: distribute zsh's history between machines in source control.
Is that something that I can achieve with zsh's history -- put history in version control the sync between machines? This is to avoid re-typing commands and quick trace.
Looks like you want BashHub:
Bashhub saves every terminal command entered across all sessions and systems and provides powerful querying across all commands.
Support for Bash and Zsh with a sweet cli for everything.
Edit: If you don't trust the person running the BashHub server for 100% and have commands that should stay private, you should run your own BashHub server.
This is an attempt to answer how to do it using, bash or zsh
In zsh the precmd function is similar to PROMPT_COMMAND in bash, in that it gets executed before each new prompt.
An example using bash:
$ function precmd() {
echo ssh copy the files to another machine
}
$ export OLD_PROMPT=$PROMPT_COMMAND
$ unset PROMPT_COMMAND && export PROMPT_COMMAND="precmd; $PROMPT_COMMAND"
ssh copy the files to another machine
$ pwd
/home/jmunsch
ssh copy the files to another machine
$ export PROMPT_COMMAND=$OLD_PROMPT
An example using zsh:
% function precmd() {
echo ssh copy the files to another machine
}
% pwd
/home/jmunsch
ssh copy the files to another machine
% unset -f precmd
Add this to the .zshrc:
function precmd() {
# across terminals, sessions
setopt APPEND_HISTORY
export machines="192.168.43.70 192.168.43.71 192.168.43.72"
for m in $machines; do
scp ~/.zsh_history $m:~/.zsh_history
done
}
For the .bashrc:
function precmd() {
# since bash generally saves history on session exit
history -a # append session to history
history -c # clear session
history -r # read from history
export machines="192.168.43.70 192.168.43.71 192.168.43.72"
for m in $machines; do
scp ~/.zsh_history $m:~/.zsh_history
done
}
export PROMPT_COMMAND="precmd; $PROMPT_COMMAND"
To make it easier to do this
.ssh/ssh_config.related:
Some assumptions about the usage here, is that only 1 person is using 1 session at a time, all machines are online, and all histories get updated successfully.
You might be able to add some additional steps to merge histories, or keep the order of histories.
Using git:
It's not a trivial problem, from what I understand. Say for example you log into two machines with the same history, but then type two different commands in at the same time; do both commands get saved, do the histories diverge, do they overwrite each other?
For further research look into distributed consistency/consensus: