up vote 8 down vote favorite
9

I routinely run multiple screen sessions on my Linux desktops and servers.

A problem with this is that when I grep through my command history, I find I issued a command in a different session, and have to detach and re-attach to get that history item.

Is it possible to 'force' the differently-updated histories from multiple sessions to all go to a central history?

If so, how?

Thanks :)

link|flag

1 Answer

up vote 8 down vote accepted

There are two things you need to do:

  1. Insert the command shopt -s histappend in your .bashrc. This will append to the history file instead of overwriting it.
  2. Also in your .bashrc, insert PROMPT_COMMAND="$PROMPT_COMMAND;history -a; history -n" and the history file will be re-written and re-read each time bash shows the prompt.

EDIT: Thanks to e-t172 for the history -n trick

link|flag
2  
Thanks for this. I improved it further using PROMPT_COMMAND="$PROMPT_COMMAND;history -a; history -n". This way, commands issued in other sessions immediately appear in the history of the current session (well, you need to press Enter first to update the history). – e-t172 Sep 8 '09 at 10:41
1  
This is wonderful! Thanks for the tip! – Nighthawk Sep 10 '09 at 12:27

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.