digital

tmux for noobs: Multiple windows and panes in your terminal

If you use Linux, and have multiple terminal windows open on your desktop, read on.

Thanks to Randall Degges who gave an introduction to tmux.

Splitting your terminal – use a a multiplexer

tmux is what’s called a terminal multiplexer.

tmux is a program you can run in your terminal. It allows you to have multiple terminal “windows” running in the one terminal window in your windows manager.

You can also split each terminal window into several panes.

This is useful if you find yourself opening multiple terminal windows.

I’m a tmux noob, so I still wasn’t sure where to start with tmux, so here’s how I got going.

The secret for using tmux

When running tmux, press Ctrl b (Linux). And then enter your tmux command.

For a list of commands, check man tmux.

I was confused by man tmux where it referred to C-b. It does explain that this means typing Ctrl b followed by command you desire.

Ok, let’s get started with tmux

  1. Open a terminal window (Ctrl Alt t)
  2. Type tmux. If the program is not installed, install it. On Debian-based systems you’ll be told how to install it with an apt-get command.
  3. Now an instance of tmux is running in your terminal. Notice the coloured toolbar at the bottom of the window which tmux will use to keep you informed.

Using tmux

Split the terminal window into two panes

man tmux tells us that the command " splits the current pane into two, top and bottom.

This means that you first press Ctrl b, and immediately followed by typing ".

Great. You now have two terminal panes to work in.

From now on I’ll use the standardised C-b notation, which means typing Ctrl b.

Open the man page in the current pane

Type:

man tmux

Now you have the tmux man page in one pane, and your standard terminal in the other pane. You should be starting to see the usefulness of tmux.

Switching panes

You can’t just use your mouse to click into the pane you want to use.

An easy way to cycle between panes in your current window is:

C-b o

Kill a pane

To kill the current pane:

C-b x

Then type y to confirm.

A task for you:

  • Read the man page to find out how to split a pane vertically rather than horizontally.
  • Split the window into two panes once again.

Create a new windows

So far, we have been dealing with one tmux window, with multiple panes.

You can also have multiple windows, with multiple panes. I consider a window like a desktop, or a browser tab.

Create a second window:

C-b c

Check the bottom toolbar. You’ll see your two windows (0 and 1) listed.

Rename your window

Make it easier to identify windows by typing:

C-b ,

(Yes, that’s a comma.)

Now type a short name to identify this window.

Switch between windows

No Alt-Tab here!

Press C-b and then the number of the window you want to open.

Try switching between window 1 and 0 and back again:

C-b 0

C-b 1

C-b 0

Cycle to the next window

Type:

C-b n

Or cycle to the previous window:

C-b p

List all keybindings

As the man page tells you, issue this command to quickly review the available keybindings:

Ctrl b ?

Stuff I still need to learn with tmux

  • How to scroll through the history of the current pane. I’m used to being able to scroll up with the mouse wheel. There must be another way to scroll, presumably with a keyboard shortcut.
  • Sessions: I know there is functionality to remember the layout of your windows and panes for the next time you run tmux.
  • Automatically launching tmux for each new terminal: once I get used to tmux, I guess I’ll want to have it launched automatically (?).

Other terminal multiplexer options

Randall mentions:

If you’ve ever had the need to run multiple terminal windows inside an existing terminal window, you’ve probably heard of GNU screen.

Unfortunately, I hadn’t.

Ok, to be more honest, I had read about screen one week before reading his blog post.

But I had been using Linux for three years before realising you could even split your terminal window.

Summary

Enjoy tmux. Was this quick run-down of help to you? What other commands have you found useful?

Syncing SSH config

You can set up SSH to connect without requiring a password each time.

SSH config

It’s handy to configure SSH connections.

For example, you can keep all SSH connections alive by default.

Edit ~/.ssh/config and add the following command:

    Host *
        ServerAliveInterval 240

Sync the config between your computers

But you still have to set that config file on your different computers.

Enter Dropbox. Store the config file in Dropbox, and point a symbolic link to it so that SSH on your computer will use that configuration.

  1. Create a folder under Dropbox, named .ssh for example.
  2. Copy ~/.ssh/config to ~/Dropbox/.ssh/
  3. Rename ~/.ssh/config to ~/.ssh/backup.config
  4. Make a symbolic link so that your Dropbox config will be found by your SSH:
    ln -s ~/Dropbox/.ssh/config ~./ssh/config

Summary

Now you can edit SSH’s config in Dropbox, and it will update on the rest of your machines.

But is there a more intelligent way of doing this?