Use tmux (a Terminal Multiplexer)

Traducciones al Español
Estamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Create a Linode account to try this guide with a $ credit.
This credit will be applied to any valid services used during your first  days.

What is tmux?

Tmux is a terminal multiplexer. It creates a host server on your Linode and connects to it with a client window. If the client is disconnected, the server keeps running. When you reconnect to your Linode after rebooting your computer or losing your Internet connection, you can reattach to the tmux session and the files you were working with will still be open, and the processes you had running will still be active.

By attaching multiple sessions, windows, and panes to a tmux server, you can organize your workflow and easily manage multiple tasks and processes.

Install tmux

Debian or Ubuntu:

Install tmux on CentOS by using the apt package manager:

sudo apt install tmux

CentOS:

Install tmux on CentOS by using the yum package manager:

yum install tmux

Mac OSX:

Install tmux on Mac OS X by using Homebrew:

brew install tmux

Creating, Detaching, and Attaching tmux Sessions

  1. Start a new tmux session with the following command:

    tmux
    

    The commands tmux new or tmux new-session can be used as well. You can create a new session with a specific name by running tmux new -s $name, replacing $name with a unique name for your session.

    Running any of the above commands will result in a new session being created with one window and one page. At this point, your terminal window should have a green menu bar at the bottom, similar to the one below:

  2. Detach from the session:

     tmux detach
    

    This will return you to the basic terminal.

  3. To attach to the last session you used, run:

     tmux attach
    

    The command tmux a can be used as well. To attach to a specific session, run tmux attach -t $name, replacing $name with the unique name given to the session you wish to use.

Once a session has been started, it will continue to run as long as the Linode is running or until you stop the session.

tmux Commands

There are three ways to issue commands to tmux:

  • shortcuts: tmux uses what is called a prefix key, which is CTRL+b by default. tmux will interpret the keystroke following the prefix as a tmux shortcut. For example: to detach from your session using a shortcut: press CTRL+b, release both keys and then press d.

  • command mode: Enter command mode by pressing the prefix followed by :. This will open a command prompt at the bottom of the screen, which will accept tmux commands.

  • command line: Commands can also be entered directly to the command line within a tmux session. Usually these commands are prefaced by tmux. The tmux attach command used in the previous section was an example of this type of command.

Most tmux tasks can be accomplished using any of these three methods.

Note
You can change the prefix key by editing the ~/.tmux.conf file. For the remainder of this guide, Prefix will be used to refer to either the default CTRL+b or the combination you have chosen in your configuration file.

Getting Help with tmux by Reviewing Keyboard Shortcuts

At any time, you can display tmux keyboard shortcuts by entering your prefix followed by ?:

Prefix + ?

Manage tmux Windows

When a tmux session starts, a single window is created by default. It is possible to attach multiple windows to the same session and switch between them as needed. This can be helpful when you want to run multiple jobs in parallel.

CommandResult
Prefix + cCreate a new window
Prefix + pSwitch to the previous window
Prefix + nSwitch to the next window
Prefix + 0-9Switch to a window using it’s index number
Prefix + wChoose a window from an interactive list
exitClose a window
Prefix + &Force kill-all processes in an unresponsive window
Prefix + %Split windows horizontally
Prefix + “Split windows vertically
Prefix + M-nSwitch between windows. Switching to a window that has a content alert, an activity or a bell
Prefix + M-pSwitch back to a previous window that has a content alert, an activity or a bell

By default, tmux names each window according to the process that spawned it (most commonly bash). To give windows names that are easier to remember and work with, you can rename a window with Prefix + ,.

Manage tmux Panes

Each window can be divided into multiple panes. This is useful when you want outputs from multiple processes visible within a single window.

CommandResult
Prefix + "Split the active pane horizontally
Prefix + %Split the active pane vertically
Prefix + arrow keySwitch to another pane
Prefix + ALT+arrowResize the active pane
Prefix + zZoom in on the active pane. Press the same combination again to exit zoom mode
exitClose the active pane
Prefix + xForce kill an unresponsive process in a pane
Prefix + kTo move the pane above
Prefix + jTo move the pane below
Prefix + hTo move the left panes
Prefix + lTo move the right pane
Prefix + qDisplay pane numbers
Prefix + oToggle / jump to the other pane
Prefix + }swap current pane with the pane from the left
Prefix + {swap current pane with the page from the right
Prefix + !move the pane out of current window
Prefix + ;Go to the last used pane
Prefix + M-1Predefined layout to switch to even-horizontal layout
Prefix + M-2Predefined layout to switch to even-vertical layout
Prefix + M-3Predefined layout to switch to main-horizontal layout
Prefix + M-4Predefined layout to switch to main-vertical layout
Prefix + M-5Predefined layout to switch to a tiled layout
Prefix + spacePredefined layout to switch to the next layout
Prefix + C-oTo move all panes, rotating the window up
Prefix + M-oTo move all panes, rotating the window down
tmux bind-key k resize-pane -U [i]To move the divider up i lines (for horizontal divider)
tmux bind-key k resize-pane -D [i]To move the divider down i lines (for horizontal divider)
tmux bind-key k resize-pane -L [i]To move the divider left i columns (for vertical divider)
tmux bind-key k resize-pane -R [i]To move the divider right i columns (for vertical divider)
C-a C-up, C-a C-down, C-a C-left, C-a C-rightTo resize panes by 1 row/column
C-a M-up, C-a M-down, C-a M-left, C-a M-rightTo resize panes by 5 rows/columns

Manage tmux Sessions

Sometimes even multiple windows and panes aren’t enough and you need to separate the layouts logically by grouping them into separate sessions. Open the command prompt with Prefix then :, then start a new session:

new-session
Note
It’s also possible to type shorter versions of a command, for example: “new-se”. But this will work only if there isn’t another command that starts with the same string of characters.
CommandResult
Prefix + (Switch to the previous session
Prefix + )Switch to the next session
Prefix + sDisplay an interactive session list
Prefix + ddetach from the current session
Prefix + $rename a session in tmux
Prefix + LSelect the most recently used session (or the last session). Use the same combination again to return.
tmux lsList all available sessions
tmux kill-serverDestroy all sessions and kill all processes

Create a tmux Configuration File

  1. As you get comfortable with tmux, you may want to change some of the defaults. Using a text editor, create a configuration file in your user’s home directory:

    File: ~/.tmux.conf
    # Uncomment the lines with the options you want to activate (by deleting the preceding "#")
    
    # Allow mouse interaction
    # set-option -g mouse on
    
    # Change prefix key to CTRL+A. "C-" stands for CTRL, "M-" stands for ALT key
    # set-option -g prefix C-a
    # unbind-key C-b
    # bind-key C-a send-prefix
    
    # Display CPU load average for the last 1,5 and 15 minutes, in the status bar
    set -g status-right "#(cut -d ' ' -f -3 /proc/loadavg) %H:%M %d-%b-%y"
  2. When you have saved your changes to this file, load the new configuration. Enter the tmux command mode by pressing Prefix then :, then use the following command:

    source-file ~/.tmux.conf
    
  3. With the mouse option enabled you can use the pointer to interact with tmux panes, windows and status bar. For example you can click on a window name in the status bar to switch to it or you can click and drag a pane line to resize it.

  4. Other configuration options are available in the tmux manual.

Servers in Tmux

Whenever you launch tmux, a server is initiated. You can connect to a tmux server under a specific socket name by using the command tmux -L <socket name>. For example, to connect to a server with the socket name “linode_socket”, run the following command:

tmux -L linode_socket

This attaches a new session. If a session already exists and want to attach to it, run the following command instead:

tmux -L linode_socket attach

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This page was originally published on


Your Feedback Is Important

Let us know if this guide was helpful to you.


Join the conversation.
Read other comments or post your own below. Comments must be respectful, constructive, and relevant to the topic of the guide. Do not post external links or advertisements. Before posting, consider if your comment would be better addressed by contacting our Support team or asking on our Community Site.
The Disqus commenting system for Linode Docs requires the acceptance of Functional Cookies, which allow us to analyze site usage so we can measure and improve performance. To view and create comments for this article, please update your Cookie Preferences on this website and refresh this web page. Please note: You must have JavaScript enabled in your browser.