tmux-plugins / tmux-resurrect

Persists tmux environment across system restarts.
MIT License
11.29k stars 422 forks source link

[Bug] Pane command history (bash history) is not saved/restored #310

Closed arderyp closed 4 years ago

arderyp commented 5 years ago

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.

arderyp commented 5 years ago

Anyone have ideas on this? Is this the actual/expected behavior?

danyg commented 4 years ago

+1

lain3d commented 4 years ago

Happens to me too.

lain3d commented 4 years ago

Is no one maintaining this repo anymore? #318 should be merged.

arderyp commented 4 years ago

unfortunately, I think this project is more or less dead

bruno- commented 4 years ago

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

0xAl3xH commented 3 years ago

Is there a plan to bring this feature back?

ioogithub commented 2 years ago

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.

nyngwang commented 1 year ago

What to do now?

BSteffaniak commented 1 year ago

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.

WoxWik commented 1 year ago

Is there any way to restore pane history with or without this plugin?

rob-levy-minimum commented 1 year ago

Only the most recently used command for each pane is really strictly necessary, I think.

That would mean that when resurrect has finished, I can just go through the panes and press ctrl+p enter (or up-arrow enter) to start running everything that was running before.

Does that make finding a solution simpler?

ioogithub commented 1 year ago

That would mean that when resurrect has finished, I can just go through the panes and press ctrl+p enter (or up-arrow enter) to start running everything that was running before.

This doesn't work. Are you referring to using the work around discussed here: BSteffaniak

rob-levy-minimum commented 1 year ago

That would mean that when resurrect has finished, I can just go through the panes and press ctrl+p enter (or up-arrow enter) to start running everything that was running before.

This doesn't work. Are you referring to using the work around discussed here: BSteffaniak

I just meant that it's the only thing I would really really need from a fix to this problem. Not the whole history but just the most recently used command in a given pane.

Svalorzen commented 12 months ago

I have to agree with @rob-levy-minimum, I'm also looking for something similar. It's a bit sad that such an obvious feature for a project like this was removed, given that people have found multiple ways to go around its possible drawbacks.