Command History and Multiple Terminal Usage
The history is saved in the file specified HISTFILE
. You can find the information contained in history in the history guide ( man history
):
typedef struct _hist_entry {
char *line;
char *timestamp;
histdata_t data;
} HIST_ENTRY;
For bash , it is common for a variable to HISTFILE
be set to .bash_history
, which is common to all shells.
Have a look at this good history reference for more hacks: The
Ultimate Guide to bash Command Line History . There you can also find information about the option histappend
marked by hek2mgl:
For example, to set this parameter, enter:
$ shopt -s histappend
And to undo it, enter:
$ shopt -u histappend
source to share
I am using the below approach here
Basically, its using python and sets
handles a unique list of all commands entered into all your shells. The result is saved in .my_history
. Using this approach, all commands entered into each open shell are immediately available in all other shells. Each cd
is stored in such a way that the file sometimes needs manual cleanup, but I find this approach better suited to my needs.
Required updates below.
.profile:
# 5000 unique bash history lines that are shared between
# sessions on every command. Happy ctrl-r!!
shopt -s histappend
# Well the python code only does 5000 lines
export HISTSIZE=10000
export HISTFILESIZE=10000
export PROMPT_COMMAND="history -a; unique_history.py; history -r; $PROMPT_COMMAND"
* unique_history.py *
#!/usr/bin/python
import os
import fcntl
import shutil
import sys
file_ = os.path.expanduser('~/.my_history')
f = open(file_, 'r')
lines = list(f.readlines())
f.close()
myset = set(lines)
file_bash = os.path.expanduser('~/.bash_history')
f = open(file_bash, 'r')
lines += list(f.readlines())
f.close()
lineset = set(lines)
diff = lineset - myset
if len(diff) == 0:
sys.exit(0)
sys.stdout.write("+")
newlist = []
lines.reverse()
count = 0
for line in lines:
if count > 5000:
break
if line in lineset:
count += 1
newlist.append(line)
lineset.remove(line)
f = open(file_, 'w')
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
newlist.reverse()
for line in newlist:
f.write(line)
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
f.close()
shutil.copyfile(file_, file_bash)
sys.exit(0)
source to share