So many issues on the given solutions:
- PREFIX, s, j/k takes too long, since it is interactive
- PREFIX, s, 0-9 isn't much better, because session indexes are not persistent over time, so you have to remember things that will change again
- PREFIX, ( / PREFIX, ) suck, because you can't reorder sessions, it's especially terrible if you have to bounce between 3 or more sessions
:switch-client -t #{session_name}
is annoying, because you have to type the full session name
The solution: Jumping to session by abbreviation
You can list all sessions, order them and grep the right one that starts with your input name, so in most cases all you have to enter is the first letter of the session name:
bind S command-prompt -p "session abbr:" "run 'tmux switch -t $(tmux ls -F \"##{session_created}:##{session_name}\" | sort -n | grep \':%%\' | head -n 1 | cut -d \':\' -f 2)'"
- It asks for the begin of a session name
- If you input nothing, you jump to your oldest session (0)
- Enter the first letter of your session name to jump to the oldest session that starts with the given letter
- If you have more sessions starting with the same letter, you can simply enter more letters to fine-tune your selection
So what's going on with the binding exactly?
run
runs a shell command
tmux switch -t
will switch to the session with the same name like the output from the result expression
tmux ls -F "#{session_created}:#{session_name}"
lists all sessions like so: 1687458684:main
- Since we output
session_created
, we can order by age with sort -n
- Then we grep the all matches with
grep ':%%'
(note the :
and our input %%
)
head -n 1
will give us only the first match
- Finally,
cut -d ':' -f 2
will return the part after :
, which is our target session name for tmux switch
Even shorter?
Just bind it to the same key as your prefix, then you can hit your prefix twice, type a letter and hit enter to jump to another session.
tmux switch -t <session name or number>
orC-b )
for forward orC-b (
for forward nice ref: tmuxcheatsheet.com – Confide