Tuesday, May 7, 2024
 Popular · Latest · Hot · Upcoming
157
rated 0 times [  157] [ 0]  / answers: 1 / hits: 86005  / 2 Years ago, fri, april 1, 2022, 1:09:01

I often open lots and lots of terminals (right now, I have seven open on this workspace) and I often search history with grep to find a command I've just written recently, but I don't want to hunt down the terminal and then scroll up and hunt for it.


Sometimes my terminals close without exit, and everything I've written in them is lost. Sometimes, I needed something I'd written in a terminal that was killed.


Is there a way to make it so that each terminal writes to .bash_history immediately? Or at least once a minute, or something like that?


More From » bash

 Answers
2

A simple solution is detailed in Update Bash History in Realtime.


It says to put those commands in the .bashrc config:


shopt -s histappend
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"

The first command changes the history file mode to append and the second configures the history -a command to be run at each shell prompt. The -a option makes history immediately write the current/new lines to the history file.


From man bash:



If the histappend shell option is enabled (see the description of shopt under SHELL BUILTIN COMMANDS below), the lines are appended to the history file, otherwise the history file is overwritten.



Related for Zsh:



[#42946] Sunday, April 3, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byishted

Total Points: 469
Total Questions: 113
Total Answers: 113

Location: Tajikistan
Member since Sun, Aug 29, 2021
3 Years ago
;