iamnotsam
iamnotsam

Reputation: 10438

Switch between sessions in tmux?

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

Upvotes: 127

Views: 101777

Answers (10)

MOHRE
MOHRE

Reputation: 1156

Ctrl + b w shows windows beside Ctrl + b s information.

Upvotes: 0

Yaser
Yaser

Reputation: 95

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

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

no need to change your tmux.conf

Upvotes: 7

Martin Braun
Martin Braun

Reputation: 12639

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.

Upvotes: 7

iamnotsam
iamnotsam

Reputation: 10438

ctrl+b, s

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

Upvotes: 213

Charlie Parker
Charlie Parker

Reputation: 5247

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/

Upvotes: 0

Zhang Buzz
Zhang Buzz

Reputation: 11088

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

Upvotes: 4

Ulrich Anhalt
Ulrich Anhalt

Reputation: 182

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.

Upvotes: 4

Furkan Siddiqui
Furkan Siddiqui

Reputation: 2055

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

Upvotes: 11

wilcus
wilcus

Reputation: 859

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

Upvotes: 12

alexgolec
alexgolec

Reputation: 28272

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

Upvotes: 62

Related Questions