In this lesson, we'll look at how to manage your history between tmux sessions, and ensure that your setup preserves your bash history between multiple windows. By adding a special PROMPT_COMMAND to your .bashrc, you can update and reload your history after every command, regardless of if you're in a different session, window, or tab.
[00:00] One of the occasionally annoying things you may have noticed is that your history in different tmux windows and panes isn't shared between them. Luckily, this is a pretty easy fix. Before you begin, let's confirm that shared history doesn't work.
[00:14] I'm going to type echo from pane 1 in the first pane. In the next pane, I'm going to type echo from pane 2. We can see that if I tab up and go through my history, we don't see the echo from pane 1 in this pane. To fix this, we're going to edit our Bash or C file.
[00:35] Depending on your system configuration of course, you might want your Bash or C file or your Bash profile, whatever the config is for the shell you're using. I'm going to zoom in on this with the prefix Z so that we can just look at this file. I'm assuming that you're using Bash.
[00:51] We're going to set shell opt -s to enable. We're going to enable hist append, which means append history, don't overwrite it. We're also going to set hist reedit, and also hist verify. These two options make sure that if editing fails due to a risk condition or something like that, we're going to try and recover from it and redo the edit.
[01:15] Next, we're going to set another magical value. This one is called hist control. I'm going to set this to ignore both. What ignore both does is it ignores commands that start with whitespace, as well duplicate commands. We don't want to add those to our history. That's extra junk.
[01:35] Finally, the real magic happens when we set prompt command. Let me add the underscore there. To start with, we're going to do history - a, which appends to history, and then history -c, which clears history. Finally, history -r, which reads from history. Then again, prompt command.
[01:59] Prompt command is this magical value. It contains a command that gets executed right before your prompt is displayed, ie after your command finished. Because of that, we can hook into here to update your history and make sure that it gets synched between different tabs, different tmux sessions, that sort of thing.
[02:17] Let's save this file. Now, let's try to source our Bash or C. Oops, that is a simple fix. History reedit, not history redit. Let's try that again. We've sourced our Bash or C in that pane. Let's go into our other pane and also source our Bash or C. Now, let's try our test again.
[02:43] In my left-hand pane, I'm going to say hi from left. In my right-hand pane, I'm going to say hi from right. Now if I go up, I see my last command and also my other one. Indeed, if I had attached from tmux completely, so now I am back in my shell. I am out of tmux.
[03:03] If I scroll up, you can see that the echo commands I ran from within my tmux session are now available in my history outside of tmux.