Skip to content

Add feature to restore paste buffers #531

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Optional:

- [restoring vim and neovim sessions](docs/restoring_vim_and_neovim_sessions.md)
- [restoring pane contents](docs/restoring_pane_contents.md)
- [restoring paste buffers](docs/restoring_paste_buffers.md)
- [restoring a previously saved environment](docs/restoring_previously_saved_environment.md)

Requirements / dependencies: `tmux 1.9` or higher, `bash`.
Expand Down
7 changes: 7 additions & 0 deletions docs/restoring_paste_buffers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Restoring paste buffers

This feature saves and restores tmux paste buffers.

It can be enabled by adding this line to `.tmux.conf`:

set -g @resurrect-paste-buffers 'on'
42 changes: 42 additions & 0 deletions scripts/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ pane_content_files_restore_from_archive() {
fi
}

# paste buffers file helpers

paste_buffers_option_on() {
local option="$(get_tmux_option "$paste_buffers_option" "off")"
[ "$option" == "on" ]
}

paste_buffers_create_archive() {
tar cf - -C "$(resurrect_dir)/save/" ./paste_buffers/ |
gzip > "$(paste_buffers_archive_file)"
}

paste_buffers_restore_from_archive() {
local archive_file="$(paste_buffers_archive_file)"
if [ -f "$archive_file" ]; then
restore_dir=$(paste_buffers_dir "restore")
mkdir -p "${restore_dir}"
gzip -d < "$archive_file" |
tar xf - -C "$(resurrect_dir)/restore/"
gzip -d < "$archive_file" | tar t | cut -d / -f 3 |
sort -r | # restore buffers in order (first saved restored last)
while read file_in_arch; do
[ ! -f "${restore_dir}/${file_in_arch}" ] && continue # skip top level dir
echo "${restore_dir}/${file_in_arch}"
done
fi
}

# path helpers

resurrect_dir() {
Expand Down Expand Up @@ -140,6 +168,20 @@ pane_contents_archive_file() {
echo "$(resurrect_dir)/pane_contents.tar.gz"
}

paste_buffers_dir() {
echo "$(resurrect_dir)/$1/paste_buffers/"
}

paste_buffer_file() {
local save_or_restore="$1"
local file_name="$2"
echo "$(paste_buffers_dir "${save_or_restore}")/${file_name}"
}

paste_buffers_archive_file() {
echo "$(resurrect_dir)/paste_buffers.tar.gz"
}

execute_hook() {
local kind="$1"
shift
Expand Down
19 changes: 19 additions & 0 deletions scripts/restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,24 @@ restore_all_pane_processes() {
fi
}

clear_paste_buffers() {
tmux list-buffers -F '#{buffer_name}' |
while read bufname; do
tmux delete-buffer -b "${bufname}"
done
}

restore_paste_buffers() {
if paste_buffers_option_on; then
clear_paste_buffers
paste_buffers_restore_from_archive | while read buffer_file; do
buffer_name=$(basename ${buffer_file##*/} | cut -d _ -f 2)
tmux load-buffer -b "${buffer_name}" "${buffer_file}"
done
rm $(paste_buffers_dir "restore")/*
fi
}

restore_active_pane_for_each_window() {
awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $9 == 1 { print $2, $3, $6; }' $(last_resurrect_file) |
while IFS=$d read session_name window_number active_pane; do
Expand Down Expand Up @@ -372,6 +390,7 @@ main() {
restore_window_properties >/dev/null 2>&1
execute_hook "pre-restore-pane-processes"
restore_all_pane_processes
restore_paste_buffers
# below functions restore exact cursor positions
restore_active_pane_for_each_window
restore_zoomed_windows
Expand Down
17 changes: 17 additions & 0 deletions scripts/save.sh
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,17 @@ dump_pane_contents() {
done
}

dump_paste_buffers() {
tmux list-buffers -F '#{buffer_name}' |
while read bufname; do
# add index prefix to save buffer order
filename=$(printf "buf%04d_%s" "${i}" "${bufname}")
i=$((i+1))
dst=$(paste_buffer_file "save" "${filename}")
tmux save-buffer -b "${bufname}" "${dst}"
done
}

remove_old_backups() {
# remove resurrect files older than 30 days (default), but keep at least 5 copies of backup.
local delete_after="$(get_tmux_option "$delete_backup_after_option" "$default_delete_backup_after")"
Expand Down Expand Up @@ -255,6 +266,12 @@ save_all() {
pane_contents_create_archive
rm "$(pane_contents_dir "save")"/*
fi
if paste_buffers_option_on; then
mkdir -p "$(paste_buffers_dir "save")"
dump_paste_buffers
paste_buffers_create_archive
rm "$(paste_buffers_dir "save")"/*
fi
remove_old_backups
execute_hook "post-save-all"
}
Expand Down
3 changes: 3 additions & 0 deletions scripts/variables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pane_contents_option="@resurrect-capture-pane-contents"
pane_contents_area_option="@resurrect-pane-contents-area"
default_pane_contents_area="full"

# Paste buffer restore options
paste_buffers_option="@resurrect-paste-buffers"

# set to 'on' to ensure panes are never ever overwritten
overwrite_option="@resurrect-never-overwrite"

Expand Down