Want a free year on Tuts+ (worth $180)? Start an InMotion Hosting plan for $3.49/mo.
One of the most widely used tools in the web development process is surely the terminal. While you are working on a project, often you find yourself in the position of using the terminal with several tabs open at once, one for launching a local web server, a second tab for managing the database, another for copying files and so on. This can soon become messy and hard to handle. One solution for this problem which works well for me, is the terminal multiplexer, tmux.
What Is Tmux
tmuxis a terminal multiplexer: it enables a number of terminals, each running a separate program to be created, accessed and controlled from a single screen.
The definition above, taken from the
tmux website, is clear: from one terminal window we can start and control a number of other terminals and in each of them run a different application or a different instance of an application.
tmux is developed on a client-server model. This brings into the game the concept of sessions. A session is stored on the server which holds the representation of a window. Each window can be linked to multiple sessions and moved between them.
On the other hand, multiple clients can be attached to each session. So, one or more users can connect to a
tmux session from different computers and they share the same information on the window. This is extremely useful in teaching or collaborating since the users sharing a
tmux session see the same information on their terminal windows.
Connecting to a
tmux session is done by starting the application with the following command:
tmux attach <session_name>
When one user wants to end the terminal sharing feature, the following command is used:
How to Install Tmux
tmux runs on Linux and Mac. At the moment of writing this article, I am not aware of a Windows version of the application.
For the majority of Linux distributions, there is a package in their repositories:
On Arch (which I use), installation is simply a matter of running the following command:
sudo pacman -S tmux
After installation, you can start
tmux by issuing the command
tmux in a terminal window. If you want to have it running automatically for each terminal session, a small bit of configuration is needed:
- In the Settings menu go to Edit Current Profile and set the Command field to
tmuxas in the screenshot below:
If you are on Mac, iTerm2 comes with
tmux installed, and to start it, you should issue the command:
After installation, if you start a terminal window, the only new thing you'll notice is the presence of a status line at the bottom of the screen:
Let's take a look at the most common features. For a list of complete features, see the links at the end of this article.
Or, in other words, splitting the main window. First of all, I must say that each
tmux command is prefixed using the following key combination:
<Ctrl-b>. This can be changed, but we will learn how to configure and customize
tmux later on.
So, in order to split a window vertically (or in right and left panes) the following command should be used:
and to split the window in horizontal panes you can use:
And the result should look like following:
Moving From One Pane to Another and Positioning Panes
In order to move the cursor from one pane to the other (activating panes), the arrow keys are used. The command looks like this:
<Ctrl-b>[Up, Down, Right, Left]
If you want to go to the previously active pane, you can use the following command:
Also, if you are not satisfied with the position of a pane, you can rotate the panes using the command:
Once created, you can change each panes size, in one cell step, using:
or in five cells step using:
Closing a Pane
When you want to close the current pane you can use:
Create a New Window
Sometimes you may want to create another window, for example, to work on another project. This window might contain a completely different set of panes with different programs in each of them. To do so, issue the following command:
Then if you want to switch to the next window you can use:
And you can switch to the previous window by using:
Or you might select the window interactively with:
Closing a Window
In order to close the currently opened window, you use:
Suppose you have issued a command on the terminal and the output of the command does not fit in one screen, so you'll need to scroll up in order to see the entire output. If you try pressing the Up key, this won't scroll you up, as it will only show you your command history. To scroll up the screen, use the following command:
And then hit one of the following keys:
PgDn to scroll up or down.
Also, when in this mode you can copy text from the history and then paste it with:
In order to exit this
insert mode, just hit esc.
Now there are a lot of other commands bound to various keys. You can list all of the key bindings by issuing:
tmux is highly configurable. The configuration file is either
/etc/tmux.conf for system wide settings or (recommended)
~/.tmux.conf for user specific settings.
Change the Prefix Key
One of the first things that most users change is the mapping of the prefix key (since
<Ctrl-b> doesn't seem to be so handy). Most users change it to
<Ctrl-a>. This can be done like so:
set -g prefix C-a unbind C-b bind C-a send-prefix
-g option in the first command tells tmux that this is a global option, meaning this is set for all windows and sessions.
Change the Key Bindings
Some users may prefer Vi or Emacs like bindings for the key actions. This is done using:
set -g status-keys vi setw -g mode-keys vi
setw command, sets the option for the window (affects all the panes in a window).
You can perform various configurations of the status line: you can turn it on or off, you can change its background and foreground color, you can change what information is displayed inside it, etc.
To turn the status bar off, issue the following command:
set -g status off
Or you may try something like this:
set -g status-bg blue set -g status-fg white setw -g status-left #H:#S at #W:#T
... which changes the status line background to blue, the text color to white and displays to the left of the status bar the hostname of localhost, followed by a colon and the session name followed by the 'at' string and the window name, a colon, and lastly the pane title.
You can also display the status line at the bottom or at the top of the window:
set -g status-position [bottom | top]
For further information on configuration and other configuration options you can check the options section of the manual.
I hope you have found this introduction to
tmux helpful in improving your terminal workflow. I've presented here just a few commands that I use most frequently. If you want to learn more, there are several resources that are available. I highly recommend: