jgoenetxea
3/6/2019 - 7:36 AM

tmux commands

How to use tmux

Manage sessions

$ tmux new -s [name of session]    # Create a session with that name
$ tmux ls    # show all active sessions
$ tmux a -t 3     # Attach to session number 3
$ tmux a #     # Attach to last created session
$ tmux a -t [name of session]    # Attach to a session with that name
$ tmux kill-session -t [sesion id]
ctrl+b d    # detach current session

Shared sessions

First, you need to have a group in common with the users you want to share. Define the session using a token:

$ tmux -S /tmp/shared_socket new -t shared_session_name

Where /tmp/shared_socker is the token and shared_session_name the name of the session. Now, ensure that the token belongs to the system group in common with the session partners.

$ sudo chgrp common_group_name /tmp/shared_socket

Then, the partners should connect to the same session using this command:

$ tmux -S /tmp/shared_socket a -t shared_session_name

If any of the partners need to be a viewer only participant, add -r in the command options to make his session read-only.

$ tmux -S /tmp/shared_socket a -t shared_session_name -r

Manage panes

ctrl+b "    # split a pane horizontally
ctrl+b %    # split a pane vertically
ctrl+b [arrow key]    # move to the pane in that direction

Copy and paste in tmux

  CTRL + b, [ => Enter ‘copy mode’
  # Go to the start position using arrow keys
  CTRL + SPACE  => to start copying.
  # Go to the end position using arrow keys  
  ALT + w or CTRL + w  => copy into Tmux buffer.
  CTRL + b, ]  => to paste in a possibly different Tmux pane/window.

NOTE: This method only stores the text in the buffer inside Tmux, not in the system buffer. So, you have to use something inside tmux to paste (a vim buffer, for example).

Advanced commands

You can activate the command prompt with:

ctrl+b :

This model lets you to add complex commands like pane resize. We can type resize-pane followed by a direction flag: -U for up, -D for down -L for left and -R for right. The last part is the number of lines to move it over by.

ctrl+b :
resize-pane -D 2

This example expands the current pane 2 lines down.

Full key binding list

(more bindings and commands in official manual page)

C-b   # Send the prefix key (C-b) through to the application.
C-o   # Rotate the panes in the current window forwards.
C-z   # Suspend the tmux client.
!   # Break the current pane out of the window.
"   # Split the current pane into two, top and bottom.
#   #  List all paste buffers.
$   # Rename the current session.
%   # Split the current pane into two, left and right.
&   # Kill the current window.
'   # Prompt for a window index to select.
(   # Switch the attached client to the previous session.
)   # Switch the attached client to the next session.
,   # Rename the current window.
-   # Delete the most recently copied buffer of text.
.   # Prompt for an index to move the current window.
0 to 9   # Select windows 0 to 9.
:   # Enter the tmux command prompt.
;   # Move to the previously active pane.
=   # Choose which buffer to paste interactively from a list.
?   # List all key bindings.
D   # Choose a client to detach.
L   # Switch the attached client back to the last session.
[   # Enter copy mode to copy text or view the history.
]   # Paste the most recently copied buffer of text.
c   # Create a new window.
d   # Detach the current client.
f   # Prompt to search for text in open windows.
i   # Display some information about the current window.
l   # Move to the previously selected window.
n   # Change to the next window.
o   # Select the next pane in the current window.
p   # Change to the previous window.
q   # Briefly display pane indexes.
r   # Force redraw of the attached client.
m   # Mark the current pane (see select-pane -m).
M   # Clear the marked pane.
s   # Select a new session for the attached client interactively.
t   # Show the time.
w   # Choose the current window interactively.
x   # Kill the current pane.
z   # Toggle zoom state of the current pane.
{   # Swap the current pane with the previous pane.
}   # Swap the current pane with the next pane.
~   # Show previous messages from tmux, if any.
Page Up   # Enter copy mode and scroll one page up.
Up, Down, Left, Right   # Change to the pane above, below, to the left, or to the right of the current pane.
M-1 to M-5   # Arrange panes in one of the five preset layouts: even-horizontal, even-vertical, main-horizontal, main-vertical, or tiled.
Space   #  Arrange the current window in the next preset layout.
M-n   # Move to the next window with a bell or activity marker.
M-o   # Rotate the panes in the current window backwards.
M-p   # Move to the previous window with a bell or activity marker.
C-Up, C-Down, C-Left, C-Right   # Resize the current pane in steps of one cell.
M-Up, M-Down, M-Left, M-Right   # Resize the current pane in steps of five cells.