Keeping bash history in sync between multiple terminals
briancarper.net
briancarper.net
It may sound weird, but look at this way: Usually I cd to a "place" to work on some particular project. When a cd to some project I haven't touched in a while, it's very useful to have, in 'history', a context – the usual commands I use to start up that server, the last few files I fiddled with, etc.
Anyone else do something like this?
https://github.com/aaronharnly/dotfiles-public/blob/master/....
(You can skip the stuff wrapping virtualenv, obviously.)
Basically just define a function that does the write, set HISTFILE, read, and then elsewhere alias cd=mycd.
If anyone gives it a try, I'd love to hear how it works! I think it needs some tweaks -- for example, multiple terminals in the same directory are probably clobbering each other -- looks like this 'shopt -s histappend' would help with that.
shopt -s histappendA good bash history can often save your behind when trying to reconstruct what you did or when you have to run that one magic command you last ran 5 months ago...
If you typically use different terminal windows for different tasks (e.g. terminal #1 for compiling, terminal #2 for manpages, terminal #3 for vim, etc.), this will just clutter up your history with items unrelated to what your are doing.
What I do it HISTFILE=$HOME/.history/`hostname`.history so then I have a nice record of what I've done on all the machines I log into. This is particularly nice when you have a central home directory.
However, reversing PROMPT_COMMAND so that "history -a" comes first makes more sense: this way the last command executed on the current terminal has a better chance of being on top of the list for the next up-arrow (which more closely resembles usual behavior).
Also, as noted above, it's better to append to PROMPT_COMMAND rather than just overwriting it. Thus, my .bashrc now reads:
export PROMPT_COMMAND="history -a; history -n;${PROMPT_COMMAND}"Found a description here: http://www.halotis.com/2010/07/06/bash-keyboard-shortcuts/
export PROMPT_COMMAND="$AUTOJUMP ; ${PROMPT_COMMAND:-:}" PROMPT_COMMAND='history -a;history -n'As people on the blog commented, I've followed and only include `history -a`. Running `history -n` would screw up the workflow of pressing the up cursor to scroll through the current terminal's history.
(I use zsh. I also downvoted you.)
Edit: How to enable in zsh: http://www.refining-linux.org/archives/49/ZSH-Gem-15-Shared-...
AUTO_CD is nice because you can just straight-up type a directory and it'll go there (i.e. .. or tilde).
HIST_IGNORE_SPACE and HIST_NO_STORE work out nicely when you want commands not to show up, i.e. stuff where maybe your password is inline, or you're handling something sensitive.
HIST_IGNORE_DUPS just saves space. HIST_IGNORE_ALL_DUPS even more so.