tmux-resurrect
tmux-resurrect copied to clipboard
tmux shell spammed with... history -r '/.tmux/resurrect/bash_history-0:0.1'
how do i stop my tmux shell from being spammed with..
history -w '/Users//.tmux/resurrect/bash_history-0:0.1' history -r '/.tmux/resurrect/bash_history-0:0.1'
i think you have enable "Restoring shell history" and this is command for save and restory history for each tmux (sub)windows...
you can disable it removing line "set -g @resurrect-save-shell-history 'on'" from your .tmux.conf
more info: https://github.com/tmux-plugins/tmux-resurrect/blob/master/docs/restoring_shell_history.md
edit: for save history after run command (more accurately after command end) you can add to your .bashrc: PROMPT_COMMAND="history -a; $PROMPT_COMMAND" this is independent on tmux, work simple with using bash, and with using tmux/tmux-resurrect is restored session in all (sub)windows have complete history from all previous (sub)windows...
Having the same problem with being spammed:
history -w '/home/cherylfong/.tmux/resurrect/bash_history-0:0.0' history -r '/home/cherylfong/.tmux/resurrect/bash_history-0:0.0'
Tried @k3dar 's solution by adding PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
to .bashrc
but this did not resolve the issue.
Having the same problem with being spammed:
history -w '/home/cherylfong/.tmux/resurrect/bash_history-0:0.0' history -r '/home/cherylfong/.tmux/resurrect/bash_history-0:0.0'
Tried @k3dar 's solution by adding
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
to.bashrc
but this did not resolve the issue.
this is only different way to store command history, but for despamming you also need: "disable it removing line "set -g @resurrect-save-shell-history 'on'" from your .tmux.conf"
I also found the spam from saving history unbearable. Yet, I didn't want to give up saving history...
I just mocked up a local patch to save the history once per day. The patch saves a file with the current date in the name to the .tmux/resurrect directory. Then, if it finds that file, it skips saving the history. To me, this is a worthwhile tradeoff---history is only saved once per day, but my console is only spammed once per day around midnight. Would it be useful if I sent the patch for review?
I just mocked up a local patch to save the history once per day
but with this patch/sollution you lost last <=23:59 history if you os crash/reboot/poweroff... with change i write above is still history save immediately after enter (respectively after entered command finish/exit)
all (sub)windows have complete history from all previous (sub)windows...
This is very undesirable, and is why i (and i suspect most others) use this in the first place. We/I want each window's pane's histories tracked separately. At least i think i do :) Ask me again after the first dozen times i accidentally close the wrong pane. Then i might want a shared global history. But for now, i want them separate.
I've found another way to keep history of commands I make inside my tmux panes:
https://askubuntu.com/a/339925
You have to preserve bash history in multiple bash shells. To do this, be sure that you have the following lines in your ~/.bashrc file:
# avoid duplicates.. export HISTCONTROL=ignoredups:erasedups # append history entries.. shopt -s histappend # After each command, save and reload history export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
it's not perfect as all panes share the same history, but it's much better than nothing
hope this helps someone
I've found another way to keep history of commands I make inside my tmux panes:
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
this reload in all tmux panels history right after enter any command, if you use only history -a (as i wite in "2" above) , then is only added and show only in new panel, but in all panels is keep history not-mixed...
Thank you @k3dar
To have different histories for different shells, you can change HISTFILE, and use $TMUX_PANE to use the pane number. I still didn't try this myself (I am using HISTFILE with other stuff, currently decided to start migrating my habits from screen to tmux), but this should work (in .bashrc):
HISTS_DIR=$HOME/.bash_history.d mkdir -p "${HISTS_DIR}"
if [ -n "${TMUX_PANE}" ]; then HISTFILE="${HISTS_DIR}/bash_history_tmux_${TMUX_PANE}" else HISTFILE="${HISTS_DIR}/bash_history_no_tmux" fi
Lazy readers: the following code doesn't work, is half answer
TL;DR: how it works and why done it this way. (Click here to see the code)
So I've been digging a bit, @didib code doesn't work as tmux
+ tmux-resurrect
seems to not be not deterministic reopening tabs. the $TMUX_PANE
is not consistent among reopenings, specially when you use several sessions. So in my trials I found that the TMUX_PANE %1 might be assigned to a different session tab 1, and if you open and close panes/windows panes that before where %25 now they might be %6 (just because you only have a total of 6 panes stored)
So I've tried this as a profile.d script (so it's run as part of .bashrc
or .bash_profile
):
# avoid duplicates..
export HISTCONTROL=ignoredups:erasedups
HISTS_DIR=$HOME/.bash_history.d
mkdir -p "${HISTS_DIR}"
# save history entries on command entered
shopt -s histappend
# After each command, save and reload history
function getHistFile() {
if [ -n "${TMUX_PANE}" ]; then
echo "${HISTS_DIR}/bash_history_tmux_$(tmux display-message -p '#S:#I:#P')"
else
echo "${HISTS_DIR}/bash_history_no_tmux"
fi
}
# intialisation
export HISTFILE=$(getHistFile);
# refresh in each command
PREV_PROMPT_COMMAND=$PROMPT_COMMAND
PROMPT_COMMAND='export HISTFILE=$(getHistFile);'
PROMPT_COMMAND+=$PREV_PROMPT_COMMAND
echo
echo "USING $HISTFILE"
echo
export PROMPT_COMMAND
So this uses tmux display-message -p '#S:#I:#P'
to generate an ID, as [SessionName]:[WindowsIndex]:[PaneIndex] and with it create a history file.
The getHistFile
function is called everytime you push a command so that if you open and close windows, in conjunction with configuration set-option -g renumber-windows on
it will keep the history file with the window and pane index that is recalculated. Also take in account without this option after reopen a session that had windows 1 and 3, now will have 1 and 2, and history of 3 (if not kept in sync) will be lost.
History files are created as expected, if you close a windows that is among others, that history files is rewritten by the window that takes is index number, although that doesn't happend until a command is pushed in that window, still a little possibility of history lost there.
The problem with this solution is that when tmux-resurrect open all the terminals, seems to do it all at once, and the command tmux display-message -p '#S:#I:#P'
returns the proper session name, but always window 1 pane 1 for single pane windows and correct windows index but last pane index for multi pane windows. So cannot properly attach history files to terminal at start.
Restoring shell history per window I think is a great feature and I would definitely appreciate if a workaround or improvement could be implemented instead of deprecating/removing the feature. If you could use a solution similar to @danyg where you do the saving via the prompt command and then just make sure the loading is done correctly that would be super helpful.
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.
I would like to only rerun commands starting with source
in each pane' s history. Is it possible now? It would be useful for restoring my anaconda/cpp environments, like running source ~/anaconda/bin/activate
on restore.
It would be great though if this could be implemented transparently by tmux resurrect for an easier/better user experience.
I think this effort should get moved out into a separate issue as a feature request. I suspect it is a top priority for a lot of people.
Many thanks for your effort.
The fact this is missing from tmux-resurrect defeats the whole point of it. I don't care about resurrecting my panes after a restart if I lose all the history in each of them.
I understand why the feature was killed, because the original implementation of reading the history after every command doesn't make sense. And it is also not desirable for every pane to share a single history file.
But tmux continuum already only saves its state on a 15 minute interval. So why not piggyback on that interval to also flush the history of each pane to its own file every 15 minutes, and then restore the contents of that file as the "history" in a new pane? We don't want to write the history of every pane to the same history file, but we want to make sure we get the in-memory history and save it somewhere that we can recover it when restoring the panes later (maybe through some kind of bash hack to "initialize" the history with our custom per-pane HISTFILE).
This functionality would be Bash-specific, and probably needs a few messy hacks.
Is there an issue or something tracking this? It seems like it could be fairly simple to implement, presumably by adding some functionality here: https://github.com/tmux-plugins/tmux-continuum/blob/master/scripts/continuum_save.sh - although it could be more complicated if there is no notion of a consistent "pane id" (I'm not familiar with how continuum/resurrect works)