Switch between sessions in tmux?
Asked Answered
T

9

119

I'm relatively new to tmux and use it just for local development. In some tmux tutorials, a person will list out their tmux sessions in an enumerated list. There is yellow highlight typically. Does anyone know what I'm talking about and how to do it? Secondly, would you say this is best practice? I'm over here with 8 iTerm2 tabs open :(

Here's a screenshot of what I'm looking for:

enter image description here

Tameika answered 25/9, 2015 at 20:19 Comment(2)
you can also do tmux switch -t <session name or number> or C-b ) for forward or C-b ( for forward nice ref: tmuxcheatsheet.comConfide
Article with precise and accurate answer - debugpointer.com/linux/switch-tmux-sessionsOpsonize
T
199

ctrl+b, s

Note ctrl+b is my prefix, your prefix could be something else.

Tameika answered 25/9, 2015 at 21:10 Comment(5)
Maybe is better tell PREFIX sWavell
Also, the command is called choose-session if you're looking to call it manually or bind another key mapping.Saeger
By now, choose-session removed, use choose-tree instead, the shortkey is <prefix> wNever
Exactly! I had like 5 sessions on. This is good because if "broken pipe" the session keeps what you were doing, lets say, in the Vim editor.Alby
I want a command, not a prefix think. I want tmux switch <session number/name>. How does one do that?Confide
X
57

You're looking for C-b ( and C-b ). You can find this and many more wonderful tips on the tmux cheatsheet.

Xmas answered 25/9, 2015 at 20:22 Comment(2)
Do you know how can set my own shortcut on these keys?Panegyrize
I want a command, not a prefix think. I want tmux switch <session number/name>. How does one do that?Confide
O
12

Is PREFIX s but the real command is choose-tree. Then you can use it to bind to other key for example to 'S'

bind S choose-tree

http://man.openbsd.org/OpenBSD-current/man1/tmux.1#choose-tree

Orthodox answered 9/11, 2017 at 22:3 Comment(1)
choose-tree lists sessions, windows, and panes. To just choose between sessions, choose-session should be used.Saeger
D
9

Use tmux switch -t [target-session] if want to switch instant

Deme answered 4/8, 2020 at 12:17 Comment(1)
[target-session] appears to be the name of the session. I wonder if there is also a way to provide the number of the session?Irritative
P
8

if you man tmux you will find a list of tmux options:

C-( Switch the attached client to the previous session.
C-) Switch the attached client to the next session.

no need to change your tmux.conf

Principally answered 16/6, 2021 at 20:48 Comment(1)
This confused me at first, as it doesn't seem to work. But when I looked at the man page, it seem to be prefix-) and prefix-(.Freesia
H
4

Ctrl-b Shift-9 and Ctrl-b Shift-0

Harass answered 20/12, 2021 at 8:24 Comment(0)
N
3

A faster switch by name is for example possible with a shell alias. For the zsh it can look as follows:

function tn() (
    if [ -n "$1" ]
      then
         tmux switch -t $1
      else
         echo "no session name"
     fi
  )

With tn go you switch to the tmux session with name go.

Navvy answered 26/11, 2020 at 11:56 Comment(0)
W
3

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.

Whitsunday answered 23/6, 2023 at 20:35 Comment(0)
C
0

You can also do tmux switch -t <session name or number> or C-b ) for forward or C-b ( for forward nice ref: https://tmuxcheatsheet.com/

Confide answered 25/9, 2015 at 20:20 Comment(1)
A more detailed article with precise and accurate answer - debugpointer.com/linux/switch-tmux-sessionsOpsonize

© 2022 - 2024 — McMap. All rights reserved.