fix: sort sessions by MRU using session_activity#216
Conversation
There was a problem hiding this comment.
Pull request overview
Updates tmux-sessionx session ordering to be true MRU by sorting sessions using tmux’s #{session_activity} timestamp, replacing the prior “last session” approach.
Changes:
- Sort initial session list by
#{session_activity}inget_sorted_sessions(). - Sort reload list after killing a session by
#{session_activity}. - Sort reload list after renaming a session by
#{session_activity}.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
sessionx.tmux |
Updates rename-session reload command to reload sessions in session_activity order. |
scripts/sessionx.sh |
Replaces client_last_session logic with MRU ordering via session_activity sorting. |
scripts/reload_sessions.sh |
Updates kill-session reload list to MRU ordering via session_activity sorting. |
Comments suppressed due to low confidence (4)
scripts/reload_sessions.sh:6
reload_sessions.shalways removes the current session when there is more than one session (grep -v "$CURRENT_SESSION"), even if the user has@sessionx-filter-currentset tofalse, and it also bypasses thefiltered-sessionsoption applied inget_sorted_sessions()on initial load. This makes the post-kill reload list inconsistent with the initial list. Consider loading@sessionx-_built-extra-options(likescripts/sessionx.shdoes) and applying the samefilter-current/filtered-sessionsfiltering during reload so behavior is consistent across reload paths.
CURRENT_SESSION=$(tmux display-message -p '#S')
SESSIONS=$(tmux list-sessions -F '#{session_activity} #{session_name}' | sort -t' ' -k1,1n | cut -d' ' -f2-)
if [[ $(echo "$SESSIONS" | wc -l) -gt 1 ]]; then
echo "$SESSIONS" | grep -v "$CURRENT_SESSION"
scripts/reload_sessions.sh:6
grep -v "$CURRENT_SESSION"treats the session name as a regex and will also exclude any session whose name merely contains the current session name as a substring (e.g.,devwould also dropdev2). Using fixed-string, whole-line matching (e.g.,grep -F+-x) avoids both regex surprises and accidental substring filtering.
echo "$SESSIONS" | grep -v "$CURRENT_SESSION"
scripts/sessionx.sh:13
- The variable name
filtered_sessioslooks like a typo (likely meant to befiltered_sessions). Even though it works, the misspelling makes the intent harder to read and increases the chance of mistakes when this logic is modified later.
filtered_sessios=${extra_options["filtered-sessions"]}
if [[ -n "$filtered_sessios" ]]; then
sessionx.tmux:91
RENAME_SESSION_RELOADrebuilds the session list by callingtmux list-sessionsdirectly, which bypasses the filtering/customization done byscripts/sessionx.sh(e.g.,filter-current,filtered-sessions, and custom paths). This can make the list change unexpectedly after a rename compared to the initial load. Consider delegating rename reload to a shared script/function that applies the same list-building logic as the initial input so all session-listing paths stay consistent.
RENAME_SESSION_EXEC='bash -c '\'' printf >&2 "New name: ";read name; tmux rename-session -t {1} "${name}"; '\'''
RENAME_SESSION_RELOAD='bash -c '\'' tmux list-sessions -F "#{session_activity} #{session_name}" | sort -t" " -k1,1n | cut -d" " -f2-; '\'''
RENAME_SESSION="$bind_rename_session:execute($RENAME_SESSION_EXEC)+reload($RENAME_SESSION_RELOAD)"
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
All four are fair points, just pre-existing and not introduced by this change. Kept the scope tight to only address the MRU ordering via |
Replace client_last_session hack with tmux's native session_activity timestamp for full MRU ordering. Fixes omerxx#210
b6dbfba to
f42492e
Compare
Summary
client_last_sessionhack (only tracked 1 previous session) with#{session_activity}timestamp sorting for full MRU orderingsession_activityformat variableHow it works
tmux list-sessions -F '#{session_activity} #{session_name}'returns a unix timestamp per session, updated whenever the user interacts with it. Sorting by this timestamp and stripping it gives true MRU order. Combined with the existing--tacflag in fzf, the most recently used session appears at the top.What stays the same
--tacflag, layout, all keybindsfiltered-sessions/filter-currentoptionsTest plan
filter-currentandfiltered-sessionsoptions still workFixes #210