tmux-resurrect: [Bug] Pane command history (bash history) is not saved/restored

I am not sure if this is a continuum or a resurrect issue)

Am I missing a configuration somewhere?

I have 5 panes set up in my session, and I have the following config:

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/resurrect'
set -g @plugin 'tmux-plugins/continuum'

# Enable autorestore
set -g @continuum-restore 'on'

# Save session every 5 minutes
set -g @continuum-save-interval '1'

# Save shell history (THIS IS BROKEN)
#set -g @resurrect-save-shell-history 'on'

# Save pane contents
set -g @resurrect-capture-pane-contents 'on'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run -b '~/.tmux/plugins/tpm/tpm'

When I reboot, then run tmux, my last session of 5 panes is restored. I even see the outputs in each pane as they appeared before reboot. However, when I navigate through each pane and use the up arrow key to re-run the most recently run command in said pane, the command is incorrect. In fact, they all show the same command (the last command I executed in any terminal session, anywhere, before reboot). So, if I rebooted from the command line with sudo reboot, the last command (hitting up arrow once) in each pane is sudo reboot.

I must be missing something, right? I’ve tried lots of googling and combing through issues here to no avail. Thanks in advance for the help.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

I have this issue too, why was the feature deprecated, it is extremely useful when using a terminal to be able to access your history.

The feature was deprecated in this commit for this reason:

Commit message from d78256:

Deprecate restoring shell history “restoring shell history” feature is very invasive and dirty. I propose removing it in the future without the replacement.

I worry it spoils the first experience of using this plugin for users that accidentally turn it on and don’t know how to turn it off, see https://github.com/tmux-plugins/tmux-resurrect/issues/288

Also, it hurts me to reject PRs that improve the feature.

Thoughts?

And it was later removed completely in this commit.

A lot of relevant discussions happened in issue #288. Specifically this comment, where a potential workaround is proposed. I have tried the solution and it does work for me.

I’ll quote the comment here for posterity:

So building off what @danyg has done I implemented a slight hack of waiting until the second prompt command before trying to load the history file, at this point the window and pane are correctly set and so it seems to work as expected when not automatically re-numbering windows (except for the first prompt, see below). Here is the modified code for the .bashrc:

# History control

# avoid duplicates..
export HISTCONTROL=ignoredups:erasedups

HISTS_DIR=$HOME/.bash_history.d
mkdir -p "${HISTS_DIR}"

function getHistFile() {
        if [ -n "${TMUX_PANE}" ]; then
                echo "${HISTS_DIR}/bash_history_tmux_$(tmux display-message -t $TMUX_PANE -p '#S:#I:#P')"
        else
                echo "${HISTS_DIR}/bash_history_no_tmux"
        fi
}

function initHist() {
        HISTFILE=$(getHistFile)
        # Only load history on the second call of this function (first time HISTINIT should be 0)
        if ((HISTINIT == 1)); then
                echo "using histfile $HISTFILE"
                # Write out any initial command given before we load the histfile
                history -a
                # Clear and read the history from disk
                history -c
                history -r
                HISTFILE_LOADED=$HISTFILE
        fi
        if [[ -n "${HISTFILE_LOADED}" && "$HISTFILE" != "$HISTFILE_LOADED" ]]; then
                echo "histfile changed to $HISTFILE"
                # History file changed (pane/window moved), write out history to new file
                history -w
                HISTFILE_LOADED=$HISTFILE
        fi
        if ((HISTINIT <= 1)); then
                ((HISTINIT += 1))
        fi
}

# initialization
HISTINIT=0

# After each command, save history
PROMPT_COMMAND="initHist; history -a; $PROMPT_COMMAND"

A detail is that if you want to use the history on the initial prompt you have to just enter a blank line or something so that the prompt command runs again, and then it should load the correct file. You can comment out the ‘using histfile’ echo if you don’t want to see that. Also note I removed the exports from dayng’s original code as it causes the prompt command to be doubled in subshells.

Automatic renumbering of windows won’t work well with this since it requires the prompt command to be rerun to recognize the history file has changed and write it out, so if there is a cascade of renaming you would have to manually rerun the prompt for each window. When moving windows manually as long as you enter a blank line or command before and after moving it should pick up the new change. Perhaps a tmux hook to send a blank input to all inactive windows (can read existing text maybe like this https://unix.stackexchange.com/a/114034/111993 and can maybe check the current active window with https://stackoverflow.com/a/42810403/583620 ) might work, could even erase the line using something like https://stackoverflow.com/a/60132582/583620 to avoid unnecessary prompt lines building up. I don’t automatically renumber windows so I haven’t investigated this.

It would be great though if this could be implemented transparently by tmux resurrect for an easier/better user experience.

Bash history restore feature is deprecated and will be fully removed soon. See https://github.com/tmux-plugins/tmux-resurrect/commit/d7825683d1ca142512254d15ed91b20593fa0d66

What to do now?