Skip to content

fix: sort sessions by MRU using session_activity#216

Open
LockeAG wants to merge 1 commit into
omerxx:mainfrom
LockeAG:fix/mru-session-ordering
Open

fix: sort sessions by MRU using session_activity#216
LockeAG wants to merge 1 commit into
omerxx:mainfrom
LockeAG:fix/mru-session-ordering

Conversation

@LockeAG
Copy link
Copy Markdown

@LockeAG LockeAG commented Mar 4, 2026

Summary

  • Replace client_last_session hack (only tracked 1 previous session) with #{session_activity} timestamp sorting for full MRU ordering
  • All three session-listing paths updated: initial load, kill-session reload, and rename-session reload
  • Zero new dependencies — uses tmux's native session_activity format variable

How 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 --tac flag in fzf, the most recently used session appears at the top.

What stays the same

  • --tac flag, layout, all keybinds
  • filtered-sessions / filter-current options
  • Preview, tmuxinator, fzf-marks, zoxide — all untouched
  • No new options, no new files

Test plan

  • Open 4+ tmux sessions, switch A→B→C→D
  • Open sessionx — D at top, then C, B, A
  • Kill a session via sessionx — list reloads in MRU order
  • Rename a session via sessionx — list reloads in MRU order
  • filter-current and filtered-sessions options still work

Fixes #210

Copilot AI review requested due to automatic review settings March 4, 2026 06:54
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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} in get_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.sh always removes the current session when there is more than one session (grep -v "$CURRENT_SESSION"), even if the user has @sessionx-filter-current set to false, and it also bypasses the filtered-sessions option applied in get_sorted_sessions() on initial load. This makes the post-kill reload list inconsistent with the initial list. Consider loading @sessionx-_built-extra-options (like scripts/sessionx.sh does) and applying the same filter-current / filtered-sessions filtering 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., dev would also drop dev2). 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_sessios looks like a typo (likely meant to be filtered_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_RELOAD rebuilds the session list by calling tmux list-sessions directly, which bypasses the filtering/customization done by scripts/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.

@LockeAG
Copy link
Copy Markdown
Author

LockeAG commented Mar 4, 2026

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 session_activity. The typo, substring matching, and reload filtering inconsistencies are worth fixing but probably better as separate PRs.

Replace client_last_session hack with tmux's native
session_activity timestamp for full MRU ordering.

Fixes omerxx#210
@LockeAG LockeAG force-pushed the fix/mru-session-ordering branch from b6dbfba to f42492e Compare March 4, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alphabetically last session is selected by default, not most recent

2 participants