106

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

iamnotsam
  • 9,470
  • 7
  • 33
  • 30

9 Answers9

172

ctrl+b, s

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

Adriaan
  • 17,741
  • 7
  • 42
  • 75
iamnotsam
  • 9,470
  • 7
  • 33
  • 30
51

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

alexgolec
  • 26,898
  • 33
  • 107
  • 159
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

wilcus
  • 739
  • 1
  • 7
  • 17
9

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

Furkan Siddiqui
  • 1,725
  • 12
  • 19
  • 2
    [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? – Martin Feb 05 '21 at 12:20
7

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

Yaser
  • 87
  • 2
  • 9
  • 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-(`. – Paul Danelli Jan 18 '22 at 17:43
4

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

Zhang Buzz
  • 10,420
  • 6
  • 38
  • 47
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.

Ulrich Anhalt
  • 172
  • 2
  • 11
2

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.

Martin Braun
  • 10,906
  • 9
  • 64
  • 105
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/

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
  • 1
    A more detailed article with precise and accurate answer - https://www.debugpointer.com/linux/switch-tmux-sessions – Lokesh Sinha Nov 04 '22 at 18:48