TranslateProject/sources/tech/20170210 Use tmux for a more powerful terminal.md

7.4 KiB
Raw Blame History

translating by Flowsnow

Use tmux for a more powerful terminal

Some Fedora users spend most or all their time at a command line terminal. The terminal gives you access to your whole system, as well as thousands of powerful utilities. However, it only shows you one command line session at a time by default. Even with a large terminal window, the entire window only shows one session. This wastes space, especially on large monitors and high resolution laptop screens. But what if you could break up that terminal into multiple sessions? This is precisely where  tmux  is handy — some say indispensable.

Install and start  tmux

The  tmux  utility gets its name from being a terminal muxer, or multiplexer. In other words, it can break your single terminal session into multiple sessions. It manages both  windows  and  panes :

  • window  is a single view — that is, an assortment of things shown in your terminal.
  • pane  is one part of that view, often a terminal session.

To get started, install the  tmux  utility on your system. Youll need to have  sudo  setup for your user account (check out this article for instructions if needed).

sudo dnf -y install tmux

Run the utility to get started:

tmux

The status bar

At first, it might seem like nothing happens, other than a status bar that appears at the bottom of the terminal:

Start of tmux session

The bottom bar shows you:

  • _[0] _ Youre in the first session that was created by the  tmux  server. Numbering starts with 0. The server tracks all sessions whether theyre still alive or not.
  • 0:username@host:~  – Information about the first window of that session. Numbering starts with 0. The terminal in the active pane of the window is owned by  username  at hostname  host . The current directory is  _~ _ (the home directory).
  • *  – Shows that youre currently in this window.
  • _“hostname” _ the hostname of the  tmux  server youre using.
  • Also, the date and time on that particular host is shown.

The information bar will change as you add more windows and panes to the session.

Basics of tmux

Stretch your terminal window to make it much larger. Now lets experiment with a few simple commands to create additional panes. All commands by default start with  Ctrl+b .

  • Hit  Ctrl+b, “  to split the current single pane horizontally. Now you have two command line panes in the window, one on top and one on bottom. Notice that the new bottom pane is your active pane.

  • Hit  Ctrl+b, %  to split the current pane vertically. Now you have three command line panes in the window. The new bottom right pane is your active pane.

    tmux window with three panes

Notice the highlighted border around your current pane. To navigate around panes, do any of the following:

  • Hit  _Ctrl+b _ and then an arrow key.
  • Hit  Ctrl+b, q . Numbers appear on the panes briefly. During this time, you can hit the number for the pane you want.

Now, try using the panes to run different commands. For instance, try this:

  • Use  ls  to show directory contents in the top pane.
  • Start  vi  in the bottom left pane to edit a text file.
  • Run  top  in the bottom right pane to monitor processes on your system.

The display will look something like this:

tmux session with three panes running different commands

So far, this example has only used one window with multiple panes. You can also run multiple windows in your session.

  • To create a new window, hit  Ctrl+b, c.  Notice that the status bar now shows two windows running. (Keen readers will see this in the screenshot above.)
  • To move to the previous window, hit  Ctrl+b, p.
  • If you want to move to the next window, hit  Ctrl+b, n .
  • To immediately move to a specific window (0-9), hit  Ctrl+b  followed by the window number.

If youre wondering how to close a pane, simply quit that specific command line shell using  exitlogout , or  Ctrl+d.  Once you close all panes in a window, that window disappears as well.

Detaching and attaching

One of the most powerful features of  tmux  is the ability to detach and reattach to a session. You can leave your windows and panes running when you detach. Moreover, you can even logout of the system entirely. Then later you can login to the same system, reattach to the  tmux  session, and see all your windows and panes where you left them. The commands you were running stay running while youre detached.

To detach from a session, hit  Ctrl+b, d.  The session disappears and youll be back at the standard single shell. To reattach to the session, use this command:

tmux attach-session

This function is also a lifesaver when your network connection to a host is shaky. If your connection fails, all the processes in the session will stay running. Once your connection is back up, you can resume your work as if nothing happened.

And if that werent enough, on top of multiple windows and panes per session, you can also run multiple sessions. You can list these and then attach to the correct one by number or name:

tmux list-sessions

Further reading

This article only scratches the surface of  tmux s capabilities. You can manipulate your sessions in many other ways:

  • Swap one pane with another
  • Move a pane to another window (in the same or a different session!)
  • Set keybindings that perform your favorite commands automatically
  • Configure a  ~/.tmux.conf  file with your favorite settings by default so each new session looks the way you like

For a full explanation of all commands, check out these references:


作者简介:

Paul W. Frields has been a Linux user and enthusiast since 1997, and joined the Fedora Project in 2003, shortly after launch. He was a founding member of the Fedora Project Board, and has worked on documentation, website publishing, advocacy, toolchain development, and maintaining software. He joined Red Hat as Fedora Project Leader from February 2008 to July 2010, and remains with Red Hat as an engineering manager. He currently lives with his wife and two children in Virginia.


via: https://fedoramagazine.org/use-tmux-more-powerful-terminal/

作者:Paul W. Frields 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出