I am experiencing the same problem as in this question: The history is shared between my iTerm2 terminal tabs: how can I switch that off?. As the user describes, my history is shared between different tabs and windows in iTerm2, which is very annoying.
I cannot use the solution suggested to the linked question, as I cannot use the setopt
command:
-bash: setopt: command not found
I am using iTerm2 Build 3.0.12 on OS X El Capitan, 10.11.6 and GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15) and tmux 2.3.
When I start only tmux, I am also getting the following error message:
tmux
-bash: iterm2_preexec_invoke_cmd: command not found
Deleting my iTerm2 profile/User Settings and starting with a "fresh", non-customized iTerm2 didn't help.
Does one of you maybe have an idea what I could try?
Thanks!
Edit:
Here is the output of shopt -p | grep histappend
:
$ shopt -p | grep histappend
shopt -u histappend
and echo $PROMPT_COMMAND
$ echo $PROMPT_COMMAND
history -a; history -c; history -r; date | xargs echo -n >>~/.bash_history_workingdir; echo -n ' - ' >>~/.bash_history_workingdir; pwd | xargs echo -n >>~/.bash_history_workingdir; echo -n ' - ' >>~/.bash_history_workingdir; tail -n 1 /Users/tabea/.bash_eternal_history >>~/.bash_history_workingdir; iterm2_preexec_invoke_cmd
My output for $PROMPT_COMMAND
looks so long, since I changed the default behaviour of the history command, in order to keep an "eternal" bash history (based on this answer). I never suspected that this could cause the weird shared-history-in-iTerm-behaviour, since I have had this in my ~/.bashrc
already before the problem started.
The relevant part of my ~/.bashrc
looks like this:
##########################################################
# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# https://stackoverflow.com/questions/9457233/unlimited-bash-history
export HISTFILESIZE=
export HISTSIZE=
export HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
export HISTFILE=~/.bash_eternal_history
# Force prompt to write history after every command.
# http://superuser.com/questions/20900/bash-history-loss
export CUSTOM_HISTFILE="~/.bash_history_workingdir" #path of the new history file
export PROMPT_COMMAND="history -a; history -c; history -r; date | xargs echo -n >>$CUSTOM_HISTFILE; echo -n ' - ' >>$CUSTOM_HISTFILE; pwd | xargs echo -n >>$CUSTOM_HISTFILE; echo -n ' - ' >>$CUSTOM_HISTFILE; tail -n 1 $HISTFILE >>$CUSTOM_HISTFILE; $PROMPT_COMMAND"
Best Answer
It's the
history -a
part ofPROMPT_COMMAND
in your.bashrc
- that's appending to the history file every time a prompt is displayed, whatever tab you're using. A quick fix would be to remove this, so that each tab's session only writes to.bash_history
when the session ends (e.g. when the tab is closed).I like this behaviour (immediate appends to
.bash_history
), and I suspect you may too! A better solution may be to maintain separate histories for each session/tab - one of the answers to this question describes how to achieve that. You could retain immediate appends, but to separate files - one for each session. You would still be able to search your entire bash history with a command likegrep "foo" ~/.bash_hist*
. (I've not personally tried this: I use one history file for all sessions, since my normal workflow tends to be in one tab or throughscreen
, and I archive my/bash_history
periodically into a separate file, as described in this article - which is well worth reading, even if it doesn't address your issue).