Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind a key to switch to last-pane, or if there are no panes, last-window?

Tags:

tmux

If I'm working in a widescreen monitor I like to primarily use two panes and switch between them with C-a Ca.

If I'm working on a square monitor I'll use two windows. I'd like to be able to switch between them with C-a C-a as well without changing my tmux.conf.

like image 409
Philip Avatar asked Sep 10 '12 00:09

Philip


People also ask

How do you toggle between windows in tmux?

Reload tmux, switch windows at least once and press: ctrl+B (default prefix) + l (lower-case L) works like a charm!

How do you toggle panes in tmux?

Tmux uses the keybinding 'Prefix' followed by 'Ctrl+o' to cycle around the panes. When you use this key-binding for the first time, it moves the pane in one position clockwise.

What is the prefix key in tmux?

By default, tmux sets the prefix key as Ctrl + b.

How do you split panes in tmux?

ctrl + b + % to make a vertical split. ctrl + b + " to make a Horizontal split. ctrl + b + left arrow to move to the left pane. ctrl + b + " to make a Horizontal split.


1 Answers

If you always want C-a to

  • switch between panes when the active window has more than one pane, and
  • switch between windows when the active window has only one pane,

then you can use an if-shell that counts the number of panes in the active window to decide between last-pane and last-window:

bind-key C-a if-shell 'test $(tmux list-panes | wc -l) -gt 1' 'last-pane' 'last-window'

It will still be “up to you” to rearrange your panes when switching between “wide” and “square” configurations (e.g. via break-pane and join-pane).


In tmux 1.8 if-shell and run-shell do format expansion, so you can simply the shell command a bit:

bind-key C-a if-shell 'test #{window_panes} -gt 1' 'last-pane' 'last-window'
like image 186
Chris Johnsen Avatar answered Sep 24 '22 20:09

Chris Johnsen