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

Deprecate restoring shell history #308

Merged
merged 1 commit into from Oct 21, 2019
Merged

Conversation

bruno-
Copy link
Member

@bruno- bruno- commented Aug 27, 2019

"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?

"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?
@bruno- bruno- self-assigned this Aug 27, 2019
@bruno- bruno- merged commit 0f0ec12 into master Oct 21, 2019
@bruno- bruno- deleted the deprecate_restoring_shell_history branch October 21, 2019 10:03
@rbren
Copy link
Contributor

rbren commented Jun 22, 2022

@bruno- I'm curious if you have any suggestions on how to replicate this behavior. It's pretty must-have for my use case.

@bruno-
Copy link
Member Author

bruno- commented Jun 23, 2022

I'm not sure. Maybe try something with the hooks feature:

execute_hook "pre-restore-pane-processes"

I'd be open to adding another hook if it helped you.

@rbren
Copy link
Contributor

rbren commented Jun 23, 2022

Adding this to my .bashrc seems to do the trick. I'm sure there's an edge case I'm not thinking of because it's dead simple. Adapted from another issue I found in this repo

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

if [ -n "${TMUX_PANE}" ]; then
  PANE=$(echo $TMUX_PANE | sed 's/%//g')
  export HISTFILE="${HISTS_DIR}/bash_history_tmux_${PANE}"
else
  export HISTFILE="${HISTS_DIR}/bash_history_no_tmux"
fi

@bruno-
Copy link
Member Author

bruno- commented Jun 24, 2022

I'm not sure if $TMUX_PANE numbers are consistent across tmux-resurrect restores.

But I'm sure you can work around that by using something like this:

${session_name}:${window_number}:${pane_index}

Of course, you'll have to get each id "component" separately via some tmux subcommand.

If you get it working consider creating a short instruction in docs/.

@rbren
Copy link
Contributor

rbren commented Jun 24, 2022

$TMUX_PANE has been pretty consistent, but I do think I saw it fail at least once. Here's a second attempt:

pane_id_prefix="resurrect_"

# Create history directory if it doesn't exist
HISTS_DIR=$HOME/.bash_history.d
mkdir -p "${HISTS_DIR}"

if [ -n "${TMUX_PANE}" ]; then

  # Check if we've already set this pane title
  pane_id=$(tmux display-message -p '#{pane_title}')
  if [[ $pane_id != "$pane_id_prefix"* ]]; then

    # if not, set it to a random ID
    random_id=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 16)
    printf "\033]2;$pane_id_prefix$random_id\033\\"
    pane_id=$(tmux display-message -p '#{pane_title}')
  fi

  # use the pane's random ID for the HISTFILE
  export HISTFILE="${HISTS_DIR}/bash_history_tmux_${pane_id}"
else
  export HISTFILE="${HISTS_DIR}/bash_history_no_tmux"
fi

To sum up - if the pane_title hasn't already been set, set it to resurrect_$RANDOM, and use that as the identifier for $HISTFILE. pane_title seems to be preserved well in save/restore.

The downside is that you can't customize pane titles, unless you use the same prefix as above (and even then renaming will wipe out your history). There's also the narrow chance of a collision.

If you think this is a decent solution I'm happy to submit a PR to /docs

@bruno-
Copy link
Member Author

bruno- commented Jun 25, 2022

Can't two panes (in different windows or in different sessions) have the same title?
And yea, if a person is actually using pane titles and actively changing them this won't work.

What would be the downside of using tmux display-message -p "#{session_name}:#{window_index}:#{pane_index}" for the identifier?

@rbren
Copy link
Contributor

rbren commented Jun 25, 2022

The issue with pane_index is that it changes as you move windows around. E.g. if you have a vertical split, and then swap the left and right panes, you also swap their pane_index, so you'd also swap history.

I thought about trying to listen for changes to pane_index but that seems pretty error-prone.

And yes - this solution would force each pane to have a unique title by using a random identifier.

@bruno-
Copy link
Member Author

bruno- commented Jun 25, 2022

If you want to do it I'm ok with submitting a PR to docs/.

I propose clearly mentioning the downsides of this approach and maybe even ask for help? I wouldn't be surprised if someone comes up with a better approach.

@jamie01
Copy link

jamie01 commented Oct 22, 2022

Thanks for this trick, seems like a fairly neat solution. I for one never use pane titles so the loss of them isn't a big deal.

I chose to modify the name generator to use the current epoch timestamp plus 1 random character, for cases where multiple panes get created within a second. This has the nice benefit to me of being clear which panes are older than others. Not sure I need that but it seemed convenient to throw it in while at it.

So my random_id=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 16) becomes
printf -v random_id %s $(date +%s) $(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 1)es

Edit: in fact, to get rid of the overhead of calling urandom entirely, we can just append milliseconds to the unix timestamp, so that leaves
random_id=$(date +%s%3N)

@seamusdemora
Copy link

Would it help to keep a separate history for each tmux session, and then add it to the overall (e.g.) bash history when the session was deleted?

@barbosa46
Copy link

When you create a session and it loads your bashrc/zshrc, for some reason, the pane_id is set to your hostname. I made this shortcut to source it again when creating a new session. If you know of a more ellegant way to deal with this, please tell me.

function tms(){
    tmux new-session -d -s $1 'source $HOME/.zshrc';
    tmux split-window;                             # split the detached tmux session
    tmux send 'source $HOME/.zshrc' ENTER;                     
    tmux send 'clear' ENTER;                     
    tmux a;
}

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

Successfully merging this pull request may close these issues.

None yet

5 participants