Techie Notes

Playing with Tmux

Tagged: linux osx tmux screen

tmux can be a very powerful substitution for gnu screen. It adds some very neat features like splitting windows into panes, and using socket for several developers to pair from their own accounts.

Cheatsheet

Tutorial

Install

OSX: bundle install tmux

Debian/Ubuntu: apt-get install tmux

type tmux to start a session, exit to get out

Prefix

Prefix is required same as in screen to distinguish tmux command from shell and applications commands. Default prefix is Ctrl-b but it can be changed to whatever you want.

Sessions

tmux to start session with default name like 1, 2 etc.

tmux new -s mysession to start a named session

Ctrl-b d to detach session

tmux ls to list existing sessions

tmux a to attach to the only running session

tmux a -t 1 to attach session 1

tmux a -t mysession to attach named session

tmux ls to see which sessions exist

to kill session:

exit from within session timux kill-session -t mysession – boy thats cumbersome

Windows

As in screen tmux can run many windows (tabs)

Ctrl-b c create new window

Ctrl-b , rename current window

Ctrl-b n move to next window

Ctrl-b p move to prev window

Ctrl-b 0 move to the window 0 (first one), same for all other numbers

Ctrl-b w interactive window menu

Ctrl-b & kill window

Panes

Tmux has a nice ability to split windows vertically or horizontally. Something like this I use in VIM a lot

Ctrl-b % split vertically

Ctrl-b " split horizontally

Ctrl-b o move to other panel

Ctrl-b Up|Down|Left|Right moving with arrow keys between panels

Ctrl-b Space circle through predefined Pane layouts

Ctrl-b x or exit kills pane

Command mode

There is a command mode to execute tmux commands (keyboard shortcuts are attached to predefined commands)

Ctrl-b : open command line allowing to enter commands

new-window -n proc "top" opens new window, names it proc, starts top

source-file ~/.tmux.conf reads configuration file

Copy and Paste

Copy and paste is a bit similar to gnu screen but more powerful. I would recommend to setup vim navigational shortcuts as it is in my config file below.

  • Start moving around: Ctrl-b [

  • Stop moving around: Enter

  • Start copy: Space

  • Copy to tmux buffer: Enter

  • Paste: Ctrl-b ]

Pair programming

It is possible to use tmux the same way as screen for pair programming if two people attach to the same session. One difference is that by default it is in sync for both users, so if one users switches to a new window – another one follows. One annoying thing though that you have to have both users in the same account. With tmux it is possible to use socket instead and it allows people from different accounts to connect to the same tmux session. Here is how to set it up…

Assume that we have users jane and john

  • create tmux group

$ sudo addgroup tmux

  • create /var/tmux

sudo mkdir /var/tmux && chgrp tmux /var/tmux && chmod g+ws /var/tmux

  • add jane and john to tmux group either by editing /etc/group file or as below

sudo usermod -aG tmux jane

  • create a session via socket

tmux -S /var/tmux/pairing

  • then attach to it from say john’s account:

tmux -S /var/tmux/pairing a

tmuxinator

tmuxinator is a very handy gem. It assists in creation of project-specific layouts using tmux.

Tmuxinator

You can install it with gem instal tmuxinator

To start a new project:

mux new proj

tmuxinator suggest some layouts for organizing panes and windows. Take in account that you can create an approximate layout using preselected ones, then add more windows and what you get in the end you can record by running in another terminal a command

tmux list-windows

the output would look something like :

1: editor* (2 panes) [119x30] [layout 1feb,119x30,0,0[119x26,0,0,0,119x3,0,27,1]] <a href='https://github.com/0' class='user-mention'>@0</a> (active)

and you can use the layout part in your tmuxinator file:

… layout: 1feb,119x30,0,0[119x26,0,0,0,119x3,0,27,1] …

Configuration

Here is my configuration file which I am sure will evolve with time