Skip to content

Commit 11c2757

Browse files
author
Philippos Sophroniou
committed
Add per-session save files/session manager script
- Modified resurrect_file_path() to save sessions using session name - Prompts user to name session if using default numeric name - Added session manager script to $HOME/bin/ on install - Interactive menu to attach, delete, or rename saved sessions - Allows multiple distinct session saves instead of single global file - Randomize temp session name with PID and $RANDOM to avoid collisions - Only kill temp session if we successfully created it - Use dynamic path for restore.sh based on plugin install location - Add Unix timestamp suffix to saved session filenames - Add custom keybinding example to README - Use find cmd to list saved sessions
1 parent cff343c commit 11c2757

File tree

3 files changed

+191
-2
lines changed

3 files changed

+191
-2
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ Automatic restoring and continuous saving of tmux env is also possible with
2929
- `prefix + Ctrl-s` - save
3030
- `prefix + Ctrl-r` - restore
3131

32+
**Custom key bindings example:**
33+
34+
If you prefer simpler key bindings (e.g., `prefix + s` instead of `prefix + Ctrl-s`),
35+
add this to your `.tmux.conf`:
36+
37+
# Save session with prefix + s
38+
bind-key s run-shell '~/.tmux/plugins/tmux-resurrect/scripts/save.sh'
39+
40+
# Restore session with prefix + R
41+
bind-key R run-shell '~/.tmux/plugins/tmux-resurrect/scripts/restore.sh'
42+
3243
### About
3344

3445
This plugin goes to great lengths to save and restore all the details from your
@@ -83,6 +94,23 @@ Add this line to the bottom of `.tmux.conf`:
8394
Reload TMUX environment with: `$ tmux source-file ~/.tmux.conf`.
8495
You should now be able to use the plugin.
8596

97+
### Session Manager (`attach` command)
98+
99+
Upon installation, tmux-resurrect creates an `attach` script in `$HOME/bin/`
100+
that provides an interactive session manager. Simply run `attach` in your
101+
terminal to:
102+
103+
- List all saved tmux sessions
104+
- Restore a session by selecting its number
105+
- Delete saved sessions (press `d`)
106+
- Rename saved sessions (press `r`)
107+
108+
**Note:** For the `attach` command to work, `$HOME/bin` must be in your PATH.
109+
Most Linux distributions include this by default, but if not, add this to your
110+
`.bashrc` or `.zshrc`:
111+
112+
export PATH="$HOME/bin:$PATH"
113+
86114
### Docs
87115

88116
- [Guide for migrating from tmuxinator](docs/migrating_from_tmuxinator.md)

resurrect.tmux

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,161 @@ set_script_path_options() {
3131
tmux set-option -gq "$restore_path_option" "$CURRENT_DIR/scripts/restore.sh"
3232
}
3333

34+
install_attach_script() {
35+
local bin_dir="$HOME/bin"
36+
local attach_script="$bin_dir/attach"
37+
38+
# Only create if attach script doesn't already exist
39+
if [[ -f "$attach_script" ]]; then
40+
return 0
41+
fi
42+
43+
# Create $HOME/bin if it doesn't exist
44+
mkdir -p "$bin_dir"
45+
46+
# Create the attach script
47+
cat > "$attach_script" << ATTACH_SCRIPT
48+
#!/bin/bash
49+
50+
# Directory where tmux-resurrect saves sessions
51+
RESURRECT_DIR="\$HOME/.tmux/resurrect"
52+
53+
# Path to restore script (set at install time)
54+
RESTORE_SCRIPT="$CURRENT_DIR/scripts/restore.sh"
55+
56+
# Check if the directory exists
57+
if [[ ! -d "\$RESURRECT_DIR" ]]; then
58+
echo "Error: Tmux-resurrect directory not found at \$RESURRECT_DIR"
59+
exit 1
60+
fi
61+
62+
# List saved sessions
63+
echo "Available saved tmux sessions:"
64+
SAVED_SESSIONS=(\$(find "\$RESURRECT_DIR" -maxdepth 1 -type f ! -name "last" ! -name "readme*" -printf "%f\n" | sort))
65+
if [[ \${#SAVED_SESSIONS[@]} -eq 0 ]]; then
66+
echo "No saved sessions found."
67+
exit 0
68+
fi
69+
70+
# Display session names
71+
for i in "\${!SAVED_SESSIONS[@]}"; do
72+
echo "\$i) \${SAVED_SESSIONS[\$i]}"
73+
done
74+
75+
echo ""
76+
echo "Options: [number] to restore, [d/D] to delete, [r/R] to rename"
77+
read -p "Enter your choice: " CHOICE
78+
79+
# Handle delete option
80+
if [[ "\$CHOICE" =~ ^[dD]\$ ]]; then
81+
read -p "Enter the number of the session to delete: " DEL_CHOICE
82+
if [[ ! "\$DEL_CHOICE" =~ ^[0-9]+\$ ]] || [[ "\$DEL_CHOICE" -ge "\${#SAVED_SESSIONS[@]}" ]]; then
83+
echo "Invalid selection. Exiting."
84+
exit 1
85+
fi
86+
DEL_SESSION="\${SAVED_SESSIONS[\$DEL_CHOICE]}"
87+
read -p "Are you sure you want to delete '\$DEL_SESSION'? [y/N]: " CONFIRM
88+
if [[ "\$CONFIRM" =~ ^[yY]\$ ]]; then
89+
rm "\$RESURRECT_DIR/\$DEL_SESSION"
90+
echo "Deleted: \$DEL_SESSION"
91+
else
92+
echo "Deletion cancelled."
93+
fi
94+
exit 0
95+
fi
96+
97+
# Handle rename option
98+
if [[ "\$CHOICE" =~ ^[rR]\$ ]]; then
99+
read -p "Enter the number of the session to rename: " REN_CHOICE
100+
if [[ ! "\$REN_CHOICE" =~ ^[0-9]+\$ ]] || [[ "\$REN_CHOICE" -ge "\${#SAVED_SESSIONS[@]}" ]]; then
101+
echo "Invalid selection. Exiting."
102+
exit 1
103+
fi
104+
OLD_SESSION="\${SAVED_SESSIONS[\$REN_CHOICE]}"
105+
read -p "Enter new name (without .txt extension): " NEW_NAME
106+
if [[ -z "\$NEW_NAME" ]]; then
107+
echo "Invalid name. Exiting."
108+
exit 1
109+
fi
110+
NEW_SESSION="\${NEW_NAME}.txt"
111+
if [[ -f "\$RESURRECT_DIR/\$NEW_SESSION" ]]; then
112+
echo "Error: A session with that name already exists."
113+
exit 1
114+
fi
115+
mv "\$RESURRECT_DIR/\$OLD_SESSION" "\$RESURRECT_DIR/\$NEW_SESSION"
116+
echo "Renamed: \$OLD_SESSION -> \$NEW_SESSION"
117+
exit 0
118+
fi
119+
120+
# Handle restore (number input)
121+
if [[ ! "\$CHOICE" =~ ^[0-9]+\$ ]] || [[ "\$CHOICE" -ge "\${#SAVED_SESSIONS[@]}" ]]; then
122+
echo "Invalid selection. Exiting."
123+
exit 1
124+
fi
125+
126+
# Restore the selected session
127+
SELECTED_SESSION="\${SAVED_SESSIONS[\$CHOICE]}"
128+
129+
# Extract the desired session name (remove .txt extension)
130+
DESIRED_NAME="\${SELECTED_SESSION%.txt}"
131+
132+
# Check if 'last' is a symlink and remove it if necessary
133+
if [[ -L "\$RESURRECT_DIR/last" ]]; then
134+
rm "\$RESURRECT_DIR/last"
135+
fi
136+
137+
# Copy the selected session to 'last'
138+
cp "\$RESURRECT_DIR/\$SELECTED_SESSION" "\$RESURRECT_DIR/last"
139+
140+
# Ensure the tmux server is running by creating a detached session
141+
temp_session="resurrect-temp-\$\$-\$RANDOM"
142+
created_temp_session=false
143+
if tmux new-session -d -s "\$temp_session" 2>/dev/null; then
144+
created_temp_session=true
145+
fi
146+
147+
# Restore the session using tmux-resurrect
148+
tmux run-shell "\$RESTORE_SCRIPT"
149+
150+
# Wait for restoration to complete
151+
sleep 2
152+
153+
# Kill the temp session only if we created it
154+
if [[ "\$created_temp_session" == true ]]; then
155+
tmux kill-session -t "\$temp_session" 2>/dev/null
156+
fi
157+
158+
# Get all sessions
159+
SESSIONS=\$(tmux list-sessions -F '#{session_name}' 2>/dev/null)
160+
161+
if [[ -z "\$SESSIONS" ]]; then
162+
echo "Error: No sessions found after restoration."
163+
exit 1
164+
fi
165+
166+
# Get the first session (the restored one)
167+
FIRST_SESSION=\$(echo "\$SESSIONS" | head -n 1)
168+
169+
# Rename the session to match the filename
170+
tmux rename-session -t "\$FIRST_SESSION" "\$DESIRED_NAME"
171+
172+
echo "Available sessions after restoration:"
173+
tmux list-sessions
174+
175+
echo ""
176+
echo "Attaching to session: \$DESIRED_NAME"
177+
tmux attach-session -t "\$DESIRED_NAME"
178+
ATTACH_SCRIPT
179+
180+
# Make it executable
181+
chmod +x "$attach_script"
182+
}
183+
34184
main() {
35185
set_save_bindings
36186
set_restore_bindings
37187
set_default_strategies
38188
set_script_path_options
189+
install_attach_script
39190
}
40191
main

scripts/helpers.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,18 @@ _RESURRECT_DIR="$(resurrect_dir)"
109109

110110
resurrect_file_path() {
111111
if [ -z "$_RESURRECT_FILE_PATH" ]; then
112-
local timestamp="$(date +"%Y%m%dT%H%M%S")"
113-
echo "$(resurrect_dir)/${RESURRECT_FILE_PREFIX}_${timestamp}.${RESURRECT_FILE_EXTENSION}"
112+
local current_session="$(tmux display-message -p '#S')"
113+
local timestamp="$(date +%s)"
114+
115+
# Check if session name is just a number (default unnamed session)
116+
if [[ "$current_session" =~ ^[0-9]+$ ]]; then
117+
# Prompt for a session name
118+
tmux command-prompt -p "Save session as:" "run-shell 'tmux rename-session %1'"
119+
# Get the new session name after renaming
120+
current_session="$(tmux display-message -p '#S')"
121+
fi
122+
123+
echo "$(resurrect_dir)/${current_session}_${timestamp}.${RESURRECT_FILE_EXTENSION}"
114124
else
115125
echo "$_RESURRECT_FILE_PATH"
116126
fi

0 commit comments

Comments
 (0)