Skip to content

Instantly share code, notes, and snippets.

@s1037989
Last active May 12, 2023 12:14
Show Gist options
  • Save s1037989/b62d21281cf57f3f0ea2c479fff2dc17 to your computer and use it in GitHub Desktop.
Save s1037989/b62d21281cf57f3f0ea2c479fff2dc17 to your computer and use it in GitHub Desktop.
Multiple bash_histories

Maintain separate bash_histories and remember the last pwd for each tmux session/window/pane!

This is useful for setting up tmux project sessions with multiple windows and panes. Each time you create a new tmux session with the same name as a previous session, each windows' pane will maintain it's own separate bash_history and pwd!

This is very useful for long-running tmux sessions, especially when something happens to any of the shells. That very complex but useful log tailer that was started 6 months ago? It's the last command that was run ... in that pane!

Pay attention to the pwd and command history for each of the split-window panes in tmux!

asciicast

Now add this to your ~/.bashrc!

function cd { if [ "$1" == -q ]; then shift; test -d "$1" || return; fi; builtin cd "$@"; echo $(pwd) > ${HISTFILE/bash_histories/pwd}; }
function history-file { echo $HISTFILE; }
function history-reconfig { history-config ; history-file ;}
function history-config {
  test -d ~/.bash_histories || mkdir ~/.bash_histories
  test -d ~/.pwd || mkdir ~/.pwd
  HISTCONTROL=ignoreboth
  HISTIGNORE="history:ls:pwd"
  HISTSIZE=10000
  HISTFILESIZE=
  HISTTIMEFORMAT="%F %T "
  LC_HISTFILE=${LC_HISTFILE/~}
  LC_HISTFILE=${LC_HISTFILE:1}
  HISTFILE=~/$(test -n "$TMUX" && tmux display -p ".bash_histories/#{host}-#{session_name}-#{window_index}-#{pane_index}" 2>/dev/null || echo ${LC_HISTFILE:-.bash_histories/$HOSTNAME})
  export LC_HISTFILE=$HISTFILE
  shopt -s histappend
  test -f ${HISTFILE/bash_histories/pwd} && cd -q "$(< ${HISTFILE/bash_histories/pwd})"
}
history-config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment