From 81982b51144181681841ca0ec4ec1337fb59a339 Mon Sep 17 00:00:00 2001 From: Radoslaw Burny Date: Mon, 13 Oct 2014 00:47:44 +0200 Subject: [PATCH 1/6] Add bash history saving and restoring (first version). This does not yet have flag to turn the feature off. --- scripts/helpers.sh | 5 +++++ scripts/restore.sh | 16 ++++++++++++++++ scripts/save.sh | 14 ++++++++++++++ 3 files changed, 35 insertions(+) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index d4e0e24..16d8090 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -63,3 +63,8 @@ resurrect_file_path() { last_resurrect_file() { echo "$(resurrect_dir)/last" } + +resurrect_history_file() { + local pane_id="$1" + echo "$(resurrect_dir)/bash_history-${pane_id}" +} diff --git a/scripts/restore.sh b/scripts/restore.sh index 77d1f35..5c714ca 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -141,6 +141,21 @@ restore_all_panes() { done < $(last_resurrect_file) } +restore_shell_history() { + awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ { print $2, $3, $7, $10; }' $(last_resurrect_file) | + while IFS=$'\t' read session_name window_number pane_index pane_command; do + if ! is_pane_registered_as_existing "$session_name" "$window_number" "$pane_index"; then + if [ "$pane_command" = "bash" ]; then + local pane_id="$session_name:$window_number.$pane_index" + # tmux send-keys has -R option that should reset the terminal. + # However, appending 'clear' to the command seems to work more reliably. + local read_command="history -r '$(resurrect_history_file "$pane_id")'; clear" + tmux send-keys -t "$pane_id" "$read_command" C-m + fi + fi + done +} + restore_all_pane_processes() { if restore_pane_processes_enabled; then local pane_full_command @@ -196,6 +211,7 @@ main() { start_spinner "Restoring..." "Tmux restore complete!" restore_all_panes restore_pane_layout_for_each_window >/dev/null 2>&1 + restore_shell_history restore_all_pane_processes # below functions restore exact cursor positions restore_active_pane_for_each_window diff --git a/scripts/save.sh b/scripts/save.sh index b8a7131..486bede 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -83,6 +83,19 @@ pane_full_command() { $strategy_file "$pane_pid" } +save_shell_history() { + local pane_id="$1" + local pane_command="$2" + if [ "$pane_command" = "bash" ]; then + # leading space prevents the command from being saved to history + # (assuming default HISTCONTROL settings) + local write_command=" history -w '$(resurrect_history_file "$pane_id")'" + # C-e C-u is a Bash shortcut sequence to clear whole line. It is necessary to + # delete any pending input so it does not interfere with our history command. + tmux send-keys -t "$pane_id" C-e C-u "$write_command" C-m + fi +} + # translates pane pid to process command running inside a pane dump_panes() { local full_command @@ -91,6 +104,7 @@ dump_panes() { while IFS=$'\t' read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_pid; do full_command="$(pane_full_command $pane_pid)" echo "${line_type}${d}${session_name}${d}${window_number}${d}${window_name}${d}${window_active}${d}${window_flags}${d}${pane_index}${d}${dir}${d}${pane_active}${d}${pane_command}${d}:${full_command}" + save_shell_history "$session_name:$window_number.$pane_index" "$pane_command" done } From 94985fc500ca01d3da13f1f42bbc5b4f16c63ddf Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Fri, 17 Oct 2014 17:45:49 +0200 Subject: [PATCH 2/6] Extract `save_shell_history` to a separate function --- scripts/save.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/scripts/save.sh b/scripts/save.sh index 486bede..a458385 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -104,7 +104,6 @@ dump_panes() { while IFS=$'\t' read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_pid; do full_command="$(pane_full_command $pane_pid)" echo "${line_type}${d}${session_name}${d}${window_number}${d}${window_name}${d}${window_active}${d}${window_flags}${d}${pane_index}${d}${dir}${d}${pane_active}${d}${pane_command}${d}:${full_command}" - save_shell_history "$session_name:$window_number.$pane_index" "$pane_command" done } @@ -116,6 +115,13 @@ dump_state() { tmux display-message -p "$(state_format)" } +dump_bash_history() { + dump_panes_raw | + while IFS=$'\t' read line_type session_name window_number window_name window_active window_flags pane_index dir pane_active pane_command pane_pid; do + save_shell_history "$session_name:$window_number.$pane_index" "$pane_command" + done +} + save_all() { local resurrect_file_path="$(resurrect_file_path)" mkdir -p "$(resurrect_dir)" @@ -123,6 +129,7 @@ save_all() { dump_windows >> $resurrect_file_path dump_state >> $resurrect_file_path ln -fs "$resurrect_file_path" "$(last_resurrect_file)" + dump_bash_history } main() { From 8328de41d892b17e430cc52081fb1c6db8ae7e46 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Fri, 17 Oct 2014 17:47:10 +0200 Subject: [PATCH 3/6] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6333b85..8b1ad68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - create `expect` script that can fully restore tmux environment - fix default save command strategy `ps` command flags. Flags are different for FreeBSD. +- add bash history saving and restoring (@rburny) ### v1.3.0, 2014-09-20 - remove dependency on `pgrep` command. Use `ps` for fetching process names. From 8684d4592bb0dd3565e19895fad2e98be388fa33 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Fri, 17 Oct 2014 22:33:29 +0200 Subject: [PATCH 4/6] Flag gate the bash history restore feature --- scripts/helpers.sh | 5 +++++ scripts/restore.sh | 4 +++- scripts/save.sh | 4 +++- scripts/variables.sh | 2 ++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 16d8090..1cb9fd1 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -49,6 +49,11 @@ remove_first_char() { echo "$1" | cut -c2- } +save_bash_history_option_on() { + local option="$(get_tmux_option "$bash_history_option" "off")" + [ "$option" == "on" ] +} + # path helpers resurrect_dir() { diff --git a/scripts/restore.sh b/scripts/restore.sh index 5c714ca..6dd1707 100755 --- a/scripts/restore.sh +++ b/scripts/restore.sh @@ -211,7 +211,9 @@ main() { start_spinner "Restoring..." "Tmux restore complete!" restore_all_panes restore_pane_layout_for_each_window >/dev/null 2>&1 - restore_shell_history + if save_bash_history_option_on; then + restore_shell_history + fi restore_all_pane_processes # below functions restore exact cursor positions restore_active_pane_for_each_window diff --git a/scripts/save.sh b/scripts/save.sh index a458385..07e3d29 100755 --- a/scripts/save.sh +++ b/scripts/save.sh @@ -129,7 +129,9 @@ save_all() { dump_windows >> $resurrect_file_path dump_state >> $resurrect_file_path ln -fs "$resurrect_file_path" "$(last_resurrect_file)" - dump_bash_history + if save_bash_history_option_on; then + dump_bash_history + fi } main() { diff --git a/scripts/variables.sh b/scripts/variables.sh index 8f451c4..f6fc88b 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -26,3 +26,5 @@ inline_strategy_token="->" save_command_strategy_option="@resurrect-save-command-strategy" default_save_command_strategy="ps" + +bash_history_option="@resurrect-save-bash-history" From f3fe4acc39034a0c638b20e0ca0b3e643d8266f5 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Fri, 17 Oct 2014 22:40:15 +0200 Subject: [PATCH 5/6] Document bash restore history feature --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 7dd0981..430323f 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,8 @@ This plugin goes to great lengths to save and restore all the details from your [configuration section](#configuration). - restoring vim sessions (optional). More details in [restoring vim sessions](#restoring-vim-sessions). +- restoring bash history (optional, *experimental*). More details in + [restoring bash history](#restoring-bash-history). Requirements / dependencies: `tmux 1.9` or higher, `bash`. @@ -130,6 +132,14 @@ Change this with: set -g @resurrect-dir '/some/path' +#### Restoring bash history (experimental) + +In `.tmux.conf`: + + set -g @resurrect-save-bash-history 'on' + +Bash `history` for individual panes will now be saved and restored. + ### Other goodies - [tmux-copycat](https://github.com/tmux-plugins/tmux-copycat) - a plugin for From 6a6d65b98a9407b9da52b58c0c23148401e400b1 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Mon, 20 Oct 2014 23:16:56 +0200 Subject: [PATCH 6/6] Update the readme --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 430323f..438e9f0 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ This plugin goes to great lengths to save and restore all the details from your - restoring vim sessions (optional). More details in [restoring vim sessions](#restoring-vim-sessions). - restoring bash history (optional, *experimental*). More details in - [restoring bash history](#restoring-bash-history). + [restoring bash history](#restoring-bash-history-experimental). Requirements / dependencies: `tmux 1.9` or higher, `bash`. @@ -138,7 +138,9 @@ In `.tmux.conf`: set -g @resurrect-save-bash-history 'on' -Bash `history` for individual panes will now be saved and restored. +Bash `history` for individual panes will now be saved and restored. Due to +technical limitations, this only works for panes which have Bash running in +foreground (as opposed to e.g. vi or top) when saving. ### Other goodies