Merge pull request #49 from tmux-plugins/bash_history

Bash save and restore history feature
pull/54/head
Bruno Sutic 2014-10-20 23:17:46 +02:00
commit 3ba092459a
6 changed files with 66 additions and 0 deletions

View File

@ -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.

View File

@ -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-experimental).
Requirements / dependencies: `tmux 1.9` or higher, `bash`.
@ -130,6 +132,16 @@ 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. 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
- [tmux-copycat](https://github.com/tmux-plugins/tmux-copycat) - a plugin for

View File

@ -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() {
@ -63,3 +68,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}"
}

View File

@ -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,9 @@ main() {
start_spinner "Restoring..." "Tmux restore complete!"
restore_all_panes
restore_pane_layout_for_each_window >/dev/null 2>&1
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

View File

@ -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
@ -102,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)"
@ -109,6 +129,9 @@ save_all() {
dump_windows >> $resurrect_file_path
dump_state >> $resurrect_file_path
ln -fs "$resurrect_file_path" "$(last_resurrect_file)"
if save_bash_history_option_on; then
dump_bash_history
fi
}
main() {

View File

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