Le 7/28/24 à 05:24, Mike Castle a écrit :
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.

[...]

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

I second Mike Castle on this.

I have more or less the same config for my bash:

export HISTSIZE=-1 # write all commands to history file
export HISTTIMEFORMAT="%Y/%m/%d %H:%M:%S "
export HISTFILESIZE=-1 #don't truncate
export PROMPT_COMMAND="history -a; history -n" # [a]ppend commands to histfile 
immedatly,
                                               # and read [n]ew entries (could 
be written from other terminals)

The PROMPT_COMMAND is where the magic happens:
Every time you press Enter,
your command is instantly recorded to the history file with history -a,
instead of waiting until bash exits properly.
This ensures you never lose track of any command,
even if your bash session ends unexpectedly.
Additionally, history -n allows your shell to read the bash history again,
updating it with any new entries.

Imagine executing foo in terminal 1,
pressing Enter,
then switching to terminal 2.
By simply pressing Enter again and hitting the Up arrow,
you'll get the command foo.

This seamless synchronization across terminals enhances my productivity by 
ensuring all terminals are always in sync.

Best,


--
yassine -- sysadm
+213-779 06 06 23
http://about.me/ychaouche
Looking for side gigs.

Reply via email to