On Sat, Jul 27, 2024 at 2:50 PM mick.crane <mick.cr...@gmail.com> wrote:
> Is this something that can be changed so history is shared between
> virtual terminals?

Yes.

There are all sorts of settings that can control how shells save
history.  Most shells are capable of doing whatever you want, but the
default configuration is likely different across each one (bash vs ksh
vs zsh vs csh), and what values you need to change to achieve what you
want varies between them as well.

Everything you need should be available through the install manual
pages, accessible via the command "man bash".  Searching for "history"
will turn up a lot of information.  The same information, organized
differently and likely different examples, is available via "info
bash" if you install the "bash-doc" package, and that same
documentation is available online at
https://www.gnu.org/software/bash/manual/ .  All forms have a section

If I'm not mistaken, by default bash has the following settings:
* Each invocation bash loads the existing history file into memory
* Each invocation keeps the last 500 commands while open
* As each invocation closes, it writes out its own history,
overwriting this existing history (e.g., the last one to exit wins)

You can verify the above yourself by opening two terminal and running
commands different command in them like:
# In one terminal
echo I am shell one

# In the other term
echo I am shell two

Then exit the shells and open up a new one, and running the command:
history | tail

to see what was saved and in what order.

You should experiment with the various settings listed in the
documentation and see what works for you for day-to-day usage.

For me, I see up bash with the following features:
* Unbounded history
* History is immediately saved to disk after each command finishes
* I keep history under source control (currently git) and regularly
(well, for some definition of "regularly"), merge them across machines

For a while, I did experiment with "immediately sharing history
between running shells."  That lasted about three months, then I gave
up on the immediate-sharing, but kept the immediate-saving.

>From my .bashrc file, I have the following history related settings:
  # No limit on running shell history size
  HISTSIZE=-1
  # No limit on lines saved in history file
  HISTFILESIZE=-1
  # Enable timestamps in bash_history
  HISTTIMEFORMAT="[%F %T %z] "
  # Stop history being clobbered if there are multiple shells open
  shopt -s histappend
  # Immediately append history
  PROMPT_COMMAND='history -a'

If you really want to try sharing history immediately between shells, use this:
  PROMPT_COMMAND='history -a; history -n'

Note that PROMPT_COMMAND executes before printing a prompt, which
means after executing a command.  So, if using "history -n", then
other shells will not load the shared history until a new prompt is
printed (e.g., hitting the ENTER key to display a new prompt).

For the record, I deal with the expected conflicts when merging
history files across machines by using a simple python program that
parses the history file (that includes the timestamps), discards the
conflict markers, orders by timestamps, and writes it back out.  It is
by no means perfect, but "good enough for me".

For those worried about the unbounded history, I started doing that
about ten years ago and my work history is currently just shy of
180,000 commands.  It would likely be less if I turned on the
"erasedups" feature, but I like to keep the context.  And I've seen
comments about folks who have multiple decades of shell history.  On
modern machines, it simply isn't an issue.

mrc

Reply via email to