Archive

Posts Tagged ‘tmux’

Using TMUX to Manage Workspace

Tmux makes management of your workspace easy, particularly when we use remote servers. I found tmux to be much more versatile than screen. Particularly the split windows are much more convenient to setup and manage. Below is a screen shot of a tmux session.

A screenshot of a TMUX session

A screenshot of a TMUX session

I started using screen when I had to code software and run experiments that would run overnight. Screen allowed me to detach the terminal from a session; I could setup and start an experiment, pack my bags, head home and later reconnect to the same session. The experiments would continue to run within the session. Had I not used screen, the sessions would have terminated when I exited the console.

Then I discovered tmux. What I like most about tmux is that I did not lose the split windows configuration when I detached from a session.

Below is a short tutorial about using tmux, which will get you started.

Tmux uses sessions, windows and panes to organize the workspace. As shown below, a session may include multiple windows, which in turn may include multiple panes. The screenshot above is an example of a window with four panes. Each pane can run an independent console instance. We can choose the number of windows we need for our workspace and number of panes within them. It is simple to configure the layout of the panes and their sizes.

Session, Windows and Panes

Sessions

To start a tmux session, we simply execute the command tmux. The session starts with one window and a pane within it. As with screen, tmux uses key combinations for shortcuts.

CTRL+b s shows all the sessions. We can do the same with tmux ls.

We can detach from a session with CTRL+b d. To reattach to the first available session we use tmux a. tmux a -t <session name> allows us to attach to a session named session name.

Windows

We can create new windows within the session with CTRL+b c. CTRL+b , renames the window. These names are visible in the status bar at the bottom of the console.

CTRL+b n and CTRL+b p moves to the next and previous windows respectively. CTRL+b w lists all windows along with their numbers. We can switch to particular window with CTRL+b <number>.

With CTRL+b & we can kill the current window.

Panes

By default a window has a single pane. To create new panes, we split planes horizontally or vertically. We can split a plane horizontally with CTRL+b % and vertically with CTRL+b ".

To move between panes we use CTRL+b { and CTRL+b } to go left or right respectively. We use CTRL+b o to switch to the next pane in the window.

CTRL+b q highlights each pane with its number.

We can rotate the consoles in the panes with CTRL+b CTRL+O

Tmux has default layouts for the panes. We can switch between those using CTRL+b space.

Commands

We can execute commands that are understood by tmux. CTRL+b : allows us to switch to command mode. For example, to resize a pane upward by 10 cells we use CTRL+b : resize-pane -U 10. The options for up, down, left and right are -U, -D, -L and -R.

Summary

If we are not sure about the key bindings, we can use CTRL+b ? to look them up. The man pages are always a good source of documentation.

Here is the list of all the key combinations I mentioned above:


tmux 		Start the session
tmux a		Attach to first available session
tmux a -t session-name
tmux ls 	List all sessions
CTRL+b  d	Detach client from current session
CTRL+b  s	List all sessions

CTRL+b  c	Create window
CTRL+b  ,	Rename window
CTRL+b  n	Next window
CTRL+b  p	Previous window
CTRL+b  w	List all windows
CTRL+b  <number> 
			Move to window number
CTRL+b  &	Kill the current window

CTRL+b  %	Split horizontally
CTRL+b  "	Split vertically
CTRL+b  q	Show pane numbers
CTRL+b  o	Next pane
CTRL+b  Ctrl-O	
			Rotate consoles within panes
CTRL+b  {	Jump to the left pane
CTRL+b  }	Jump to the right pane
CTRL+b  <space> toggle through the different layouts

CTRL+b  :	Enter command mode
CTRL+b  : resize-pane -U 10 
			Resize the pane upwards

CTRL+b  ?	Show key bindings

Categories: best practices Tags: ,