Basics of tmux and why you should care
I used to work often on remote machines. Whenever I needed to do something in a different window while still maintaining the state of the currently open one — say, running a long terminal blocking task and editing a file simultaneously — I would open a new terminal tab/window and a whole new SSH connection. While it gets the jobs done, that can be a bit inefficient what with latency and all. This changed when I started using tmux.
tmux stands for terminal multiplexer. A major use case for tmux is providing access to several terminal sessions through a single window. Most GUI terminal programs like iTerm have this functionality and as most programmers often do, you probably have at least a dozen terminal tabs open right now.
My terminal tabs. tmux allows you to achieve this same tab abyss but on the CLI.
Our focus will be on using tmux to manage windows and be more productive. You can find instructions for installing tmux at the end of this article.
- We'll SSH into our remote machine.
2. To get started, we'll run
$ tmux to start a new session. We should see a green bar appear at the bottom:
3. Now let's say we have some long running job that we want to remain in the foreground:
## this echos 'Running some job' to stdout once every second $ while true; do echo 'Running some job' && sleep 1 ; done
4. If we need to edit a file using vim or some other editor, this terminal is blocked by our running job. To run other commands, we need to open a new window which we can do by pressing the following keys in sequence
At the green status bar on the bottom, we can see on the left there's now two windows open 0:bash and 1:bash. 1:bash has an asterisk * next to it to tell us it's the window we're currently viewing.
We'll go ahead and run
$ touch code.js to create the file and
5. We have a problem now. We're trapped in vim and can't get out! While we Google how to exit vim, we decide to go back to the job we were running on step 3 to see how it's progressing.
0 is the identifier for the window we were running our job 0:bash so to switch back to that window, we'll do
Ctrl+b " or vertically
Ctrl+b %. We'll split it vertically and run our script on the new pane:
To switch between the two panes, we can press
Ctrl+b <arrow-left> or
7. Finally, we'll close the extra pane
When you need to look up different commands and what they do, you can
Ctrl+b ?. You may have noticed that all the commands we put in were prefixed with
Ctrl+b. This is known as the prefix key and whatever comes after it is known as the command key. Thus we scroll down to the section with "bind-key -T prefix" on the help page:
Alternatively, this cheatsheet also comes in handy.
These are a few of the basic commands that'll help you get going, there are dozens more. You can get by knowing only the commands in this article. You may find it a bit difficult to get used to tmux at first and frustrating to keep looking up commands but in a short while, they'll become second nature!
## Ubuntu / Debian $ sudo apt-get install tmux ## CentOS $ sudo yum -y install tmux ## MacOS $ brew install tmux
Really interesting! Thanks for posting this. :-)