您的位置:首页 > 其它

tmux简单使用

2018-03-15 16:59 381 查看
原文: https://www.sitepoint.com/tmux-a-simple-start/ Sometime in the last year or so, I started using another development transformer: Tmux . What is tmux? According to its site:
tmux is a terminal multiplexer. What is a terminal multiplexer? It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. And do a lot more.I first learned about tmux from Evan Light (@elight) when I engaged him topair program with me eons ago. I was stunned at how easy tmux made it for Evan and I to collaborate in real-time. My reaction was a bit like an unfrozen caveman. I marvelled at the technology, presuming only advanced beings, like Evan, could wield such power. I put it on my “Someday” list and went back to my stone tablets.

In all likelihood, you’ve probably already heard of tmux. However, you may not be using it everyday. If tmux is on your “Someday” list because you think it is too complicated (I mean, c’mon, the word “multiplexer” is just plain scary), then I am here to show you just how easy it is to put tmux into your workflow.

Simple Install

To start, you’ll need tmux (DERP). Depending on your platform, there are various (easy) ways to perform the install:

Mac OSX

Use Homebrew
brew install tmux
 and you’re done.

Ubuntu/Debian

There is a Ubuntu package so, 
sudo apt-get install tmux
 will work, but the installed package is 1.7 and we really want 1.8. To get 1.8, do the following (thanks to this article):
$ sudo add-apt-repository ppa:pi-rho/dev
$ sudo apt-get update
$ sudo apt-get install tmux
It is not a huge deal if you cannot get 1.8 or don’t want to go through adding a PPA. You’ll just miss out on one of the cool features added in 1.8, which I will explain later.

Windows

No dice, unless you use Cygwin or install a VM with a *nix distrubution and then follow the instructions for Ubuntu.Type the following to make sure all is well.
$ tmux -V
tmux 1.8
Hurray! tmux is installed!

Simple Usage

Using tmux is as easy as typing 
tmux
. When you do that, it just looks like your screen clears and you’re back at the same terminal prompt. A bit anti-climatic, maybe, but you’ve just stepped through the looking glass.

The more observant among you will notice that the terminal how has a green bar along the bottom.

If you are thinking that this is a status line for tmux, you’d be right. Right now, it tells me the date, time, the host, what “windows” are open (just 1 named “0:bash”), and the name of the session (“0”).When you start tmux with no arguments, it names the session using a zero-based list that increments by one for every current tmux session. As you might have guessed, you can name your sessions whatever you like, but we won’t get into that today.The status bar can also be configured to display more information, use different colors, and many (MANY) other things. In this article, however, we are only going to focus on windows and panes.

No Pane, No Gain

I’ve already mentioned words like “session”, “window”, and “pane”. When you run tmux, it creates a new tmux session. A tmux session is, put simply, and container for windows and panes. A window contains one or more panes.

Each pane and each window are a virtual terminal, it just happens to be that windows can contain panes. Panes and windows can swap places, as well, so a pane can be made a window and window can be made a pane.When starting life with tmux, I’d recommend you split your terminal into 1-3 panes for an “edit” window, and run other processes (like a rails console or tailing a log file) in their own windows. That is what we will walkthough today.

Windows into Your Application

Let’s do this. Fire up a terminal and 
cd
 into the root of your application. I am using a Rails app here, but that is not important.Now, type 
tmux
.This is the first window, which will be the “edit” window for our session. I want to see the editor, my tests, and what 
top(1)
 has to say about current affairs. Split the current window into two panes by typing:
CTRL+B %
Whoa! That key-combination is a bit cryptic, no? It is, but that’s OK. Learning the tmux commands is no different than learning anything else, and they can be easily changed.It is worth noting,however, that the 
CTRL+B
 portion of that macro is called “the prefix”. 
CTRL+B
 is the default prefix for tmux, but you won’t be surprised to know that most people change it.Your terminal should now look like:

Now, we want to split the right-hand pane into 2 panes that are stacked vertically. Your cursor should be in the right-side pane. Type:
CTRL+B "
and your terminal should look like:

With the panes setup, the commands for each pane need to be launched. Since your cursor is now in the bottom, right-side pane, type 
top
 and watch top fire up. OK, switch to the top, right-side pane and type a command. For me, I’ll use 
rake test
, but I would generally make this pane run the automated tests using guard or something similar.Before you can do that, though, you need to know how to switch panes, don’t you? To move from the bottom, right-side pane to the top, right-side pane:
CTRL+B <UP ARROW>
Same prefix, new suffix to the command. This one makes a bit more sense; to move up, use the up-arrow. Now type the command to fire up your tests.Can you figure out how to move to the left-side pane? Once you do, launch your editor. I use VIM, so I type 
vim
.Here is my edit pane:

OK, the edit pane is done. Pro tip: If you are using tmux 1.8, you can toggle the curent pane into full-screen by typing:
CTRL+B  z
I LUUUUUV this feature, because it means I can full-screen my tests when they are failing and navigate around that pane more easily, then return it to its original size when done.Quickly now, I’ll show you how to add a window so you can fire up other services related to your app in the tmux session. To add a new window:
CTRL+B c
You are now in a pane-free (HA!) terminal and the status bar shows two windows (0 and 1). Fire up the rails console or whatever you like here.

Add as many windows as you like using that same key combination. When you want to switch between windows 0 and 1, type:
CTRL+B <window number>
where is 0 or 1.Now you can jump back and forth at your heart’s consent.

The End is Near

The last thing I’ll mention is how you detach from and end a session. When you are in a session, you are “attached” to it. The 
tmux
 command creates a new session and attaches the current terminal. To detach, type:
CTRL+B d
You’ll see something like 
[detached]
 and you’ll be back at your no-status-bar, regular, old terminal. If you type 
tmux ls
, you will see a list of sessions:

All the stuff you had running in that session is still cruising right along. You can reattach to it by typing:
tmux attach -t 0
where 
-t
 stands for “target” and 
0
 is the session name. Now, go ahead and detach again.To end/kill the session, type
tmux kill-session -t 0
This will end the session, so if you do another 
tmux ls
, you will see zero sessions running. By the way, if you 
exit
 out of all the windows in a session, the session will exit as well.

Next Steps

This article just skims the surface of what tmux can do for you. However, you can apply what you learned today and be more productive in your development environment without investing another second of time learning about tmux.But, you aren’t that kind of person, are you? Once you get comfy with what you’ve learned here, this is what I recommend you do next:Buy the Tmux Book and read it. It’s great.
Create your .tmux.conf file and start changing tmux to suit your needs. There are LOADS of examples and it makes tmux even better.
Check out tmuxinator for setting up named tmux environments that fire up a bunch of panes and windows in a new session with a simple command.
Pair program with someone using tmux as the way to collaborate in real-time. It’s mind-blowing.

Wrap Up

The goal of this article was to get you going with tmux in as simple a way as possible. I am now at the point where I start tmux up to do just about everything. I love convenience of splitting a window into panes and running a quick command without leaving my current window. I think you’ll find it just as useful.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: