Tuesday, July 9, 2013

Tmux basics

Using tmux. What is tmux? How does it work and what's it useful for? Someone once recommended tmux as something useful to use. So I started looking into it without really knowing what it was. Here I'll tell you how it's actually useful to me. So, what is it? Here's what they have to say about it on sourceforge. "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. See the manual."

Well that didn't make much sense to me and it took a bit of searching around and reading manuals and the like to find out how tmux would actually be useful to me. 
For me tmux is pretty much just a way to run multiple terminals inside of one. Here's mine with 3 terminals open:

It just looks like one of course(you can only type into one at a time). If you look at the green bar at the bottom you can see the names of each: 0:bash, 1:bash~, 2:bash*
So far this hasn't impressed me. I mean, that's just like having different tabs right? I can even do that with the terminal so that's not useful unless maybe I was on a server install. -- True. Where tmux really becomes useful is with it's ability to detach and attach sessions. I have 3-4 different computers(one isn't on too much) and I'm too lazy to get up and go to each computer when I find something cool that I want to install on all my computers, or when they all need to be updated. A lot of the time when I am at home it's not a problem to simply leave a terminal open connected to one of the computers via ssh while it installs something/updates/while running a script. Although if I decide I do want to get off my lazy but and go to one of the other computers(for instance from my laptop to my desktop) then I have to make sure the laptop stays up and running, or I have to kill the command and rerun it on the computer when I get there. If I am using tmux however I can simply detach the session, and reattach it when I get to the other computer. 
Where I find I get the most use out of tmux is when I am connecting to a school server, or when I am connecting on my laptop from somewhere other than home. When connecting from somewhere other than home(and especially if I am installing something/running a script that will take a while I can't be sure that my in-laws internet will stay connected the whole time(if you look a the box(modem) wrong it disconnects). With tmux I can start apt-get then detach and it will be running in the background the whole time... and I don't need to be connected to that computer via ssh.
So now that I've bored you all with the usefulness of tmux for me, here's a few simple commands that I use that can hopefully get you started. If you start to find it useful and want to use it more I recommend going to one of the other sites out there to see more things you can do with it.
To start tmux simply type the command:
    tmux
To open a new terminal instance:
    Ctrl+b c
To close a terminal:
    Ctrl+b x
To go to the next terminal:
    Ctrl+b n
To detach the session:
    Ctrl+b d
To reattach after detaching use command:
    tmux attach

UPDATE: Wow I just realized this post is pretty long. For a crash course just install tmux and play with the above commands in the last paragraph. After learning and using tmux a bit more I'd say its time to add some more commands.

I simply looked at the man pages and created a cheat sheet of just the commands here.

Some of the main ones I'm using aside from the above are:
To split terminal  vertically:
   Ctrl+b %
To split horizontally:
   Ctrl+b "
To navigate through existing panes:
   Ctrl+b LEFT,RIGHT,UP,DOWN
Holding Ctrl+b down while using the navigation buttons will resize your current pane.

No comments:

Post a Comment