Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
arderyp opened this issue Sep 27, 2019 · 15 comments
Closed

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

arderyp opened this issue Sep 27, 2019 · 15 comments

Comments

@arderyp
Copy link

arderyp commented Sep 27, 2019

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
Copy link
Author

arderyp commented Oct 1, 2019

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

@arderyp arderyp changed the title Pane command history is not saved Pane command history (bash history) is not saved/restored Oct 1, 2019
@arderyp arderyp changed the title Pane command history (bash history) is not saved/restored [Bug] Pane command history (bash history) is not saved/restored Oct 17, 2019
@danyg
Copy link

danyg commented Jan 14, 2020

+1

@lain3d
Copy link

lain3d commented Apr 17, 2020

Happens to me too.

@lain3d
Copy link

lain3d commented Apr 17, 2020

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

@arderyp
Copy link
Author

arderyp commented Apr 18, 2020

unfortunately, I think this project is more or less dead

@bruno-
Copy link
Member

bruno- commented Aug 23, 2020

Bash history restore feature is deprecated and will be fully removed soon.
See d782568

@bruno- bruno- closed this as completed Aug 23, 2020
@0xAl3xH
Copy link

0xAl3xH commented Apr 2, 2021

Is there a plan to bring this feature back?

@ioogithub
Copy link

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
Copy link

nyngwang commented Apr 2, 2023

What to do now?

@BSteffaniak
Copy link

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
#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
Copy link

WoxWik commented May 15, 2023

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

@rob-levy-minimum
Copy link

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
Copy link

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
Copy link

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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests