This is an article that I started more than two years back (to be precise, on January 2012) and kept in my Google drive without touching for a long time. But I have been using tmux ever sice I found it and it's a great productivity tool for me. You can make it more awesome with some configurations and plugins. In this article we'll see the basic usage of Tmux.
Tmux, which is the abbreviation of Terminal Multiplexer, is a very useful utility that allows us to emulate multiple independent terminals in single screen/window/tab. We can run separate programs on each terminal. It is very easy to switch from one terminal to other. tmux commands are identical as shell commands, so we can use all shell commands from tmux interface.
To install tmux
sudo apt-get install tmux // ubuntu brew install tmux // Mac
Tmux basic concepts
There are some basic concepts which you should know before start using this tool. Tmux has three level of hierarchy for handling multiple terminals. Session > Windows > Panes.
Session A Session is a group of tmux windows. Session is ideal for grouping or separating your workspaces. You can configure a session to have a base directory and all new windows/panes in the session to start from that directory. You can connect to a tmux session from a different terminal or even from a remote machine.
After installation, you can start new tmux session from the commandline with hitting tmux in the commandline.
It will start a new tmux session where you can create multiple windows. Each tmux session will have a name, by default which will be a number starting from 0 (Zero). Or you can give it a name while starting new seesion.
$ tmux new -s node
This will start a new session with name
node. Session name is useful for connecting (attaching a terminal) to existing sessions. You can list existing sessions with
$ tmux list-ssions node: 1 windows (created Sat May 3 10:12:28 2014) [149x41] (attached)
To attach to existing session from somewhere else
$ tmux attach-session -t node
You can attach to sessions from any Ternminal tabs and windows and you'll see exactly the same in both places. This helps you to share your terminal with other developers and they can remotely work on your machine, while you can see what they are doing.
Windows Windows helps to layout multiple panes (terminals) in one screen. A window is samilar to a tab in native Terminals. Each tmux session starts a new window also. Windows will have a name, which'll be displayed in the bottom (status bar).
Panes A pane is a multiplexed terminal. Ideally you'll be using tmux for creating multiple panes in one window and moving from one pane to another for doing different tasks. There are many commands for managing windows and panes. It's actually not commands, we'll call them key binding. We are basically binding a key for each task that we want to do with tmux. We'll go through them below.
Tmux also has a config file
~/.tmux.conf where you can define your tmux configuration.
Apart from the bind keys you should use a prefix key to tell tmux what you want to do. By default it's
Ctrl + b, which I changed to
Ctrl + a for ease of use. It can be done in config file by setting
set -g prefix C-a unbind C-b bind C-a send-prefix
Here are some of the commonly used key bindings
Ctrl-a c Create new window Ctrl-a d Detach current client Ctrl-a l Move to previously selected window Ctrl-a n Move to the next window Ctrl-a p Move to the previous window Ctrl-a & Kill the current window Ctrl-a , Rename the current window Ctrl-a % Split the current window into two panes Ctrl-a q Show pane numbers (used to switch between panes) Ctrl-a o Switch to the next pane Ctrl-a ? List all keybindings
Adding more awesomeness with Teamocil
Teamocil is an awesome tool for simplifying your workflows with tmux. You can use it for automatically creating windows and panes and configure it to execute some commands when the session is started. To install Teamocil, run
$ gem install teamocil
Most probably, you will have different workflows for office and personal projects. Teamocil lets you to create predefined configuration for each case and you can setup your entire workspace just by launching the teamocil. You can create the teamocil configuration like
Say for example, when I start my work at office, I'll first move to my projects folders and make an update from version control systems. In some other terminals I'll connect to different servers, and so on. With Teamocil, I'll create a configuration file
office.yml for office with
$ teamocil --edit office and add the following content
session: name: "Office" windows: - name: "Local" root: "~/Workplace/office" layout: tiled panes: - cmd: ["ls"] focus: true - cmd: ["cd application; svn up ."] - cmd: ["cd monitor; git pull origin master"] - name: "Remote" root: "~/Workplace/office" layout: tiled panes: - cmd: ["ssh -i prodkey.pem firstname.lastname@example.org"] focus: true - cmd: ["ssh -i testkey.pem email@example.com"] - cmd: ["ssh -i devkey.pem firstname.lastname@example.org"]
To launch this, first start a tmux session and from inside the tmux session run,
$ teamocil office
It will make the workspace ready in a few seconds. You can find more about the settings in their documentation.
Tmux, by default has many limitations and you won't find it as friendly as working with multiple tabs. Like mouse scroll is not enabled, copy-paste not working, etc. But you can overcome those limitaions via some configurations or using some plugins.
Ideally, you won't have to use mouse while working with tmux. Just in case you need, here are some useful configs related to mouse.
setw -g mode-mouse on # enable mouse scroll set -g mouse-select-pane on # use mouse to select pane set -g mouse-resize-pane on # use mouse to resize pane
Here is an article that explain how to enable copy & paste on osx.
In this article we've seen how to setup and use a great productivity tool - Tmux. Here we've discussed only the basics. There are plenty of things that you can do with it. Explor, learn and enjoy! :)