2

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"
  • setopt is zsh-specific - the bash equivalent is shopt. Could you run shopt -p | grep histappend and report back what you see? Also, could you run echo $PROMPT_COMMAND and report that, too? – John N Dec 2 '16 at 9:05
4
+50

It's the history -a part of PROMPT_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 like grep "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 through screen, 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).

  • Thanks a lot, that fixed it! I haven't modified my .bashrc yet to to write into separate history files, as suggested as in the other posts, so I can't comment on how it is working. But it has solved my problems of shared histories. – TabeaKischka Dec 2 '16 at 16:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .