Tmux – Terminal Multiplexer

I recently discovered tmux which is a terminal multiplexer, or you can consider it a "window" manager for the terminal. I've been a user of GNU screen for a long time, but its development is a little stale. So worth checking out the new kid who promises new features and improved usability.

This article is just some notes on how to use tmux, which is the only way to really learn anything

Basics

Start a screen: $ tmux

Once start, you issue commands using ctrl-b (default), this is called the "prefix". You can also map another key, I'm used to using ctrl-w in screena nd windows, so I continue using it in tmux.

TO rebind keys: create ~/.tmux.conf

set -g prefix C-w 

Commands

ctrl-w c Create New Window
ctrl-w n Next Window
ctrl-w p Previous Window
ctrl-w 0-9 Switch to Window #
ctrl-w d Detach from current session (it's still there)

Detach and Reattach

You can detach from a tmux session using ctrl-w d ; All your screens will still exist, exactly how you let them, with all running processes. You can even close your terminal sesssion, ie. disconnect if you were logged in remotely.

You can reattach using: $ tmux attach

After reattaching, everything will be as you left it. This is amazing when working with remote hosts, especially if you get dragged away to a meeting and unplug your network and forget to disconnect or lost connection due to flaky wifis. You can log back in, reattach and you're exactly how you left it.

Scrolling

Screen is its own window manager and operateas outside of the system windowing, which makes scrolling a bit more difficult. First make sure you define a good chunk of lines to scrollback in your screenrc. Using defscrollback 5000

Next to enter scrollback mode use ctrl-w [ and then to control scrolling it is standard vim commands.

h,j,k,l - cursor movement
ctrl-b  - back a page
ctrl-f  - forward a ag
/       - search forward
?       - search backward

Split Windows

ctrl-w " Split screen horizontally
ctrl-w % Split screen vertically
ctrl-w o Navigate between splits
ctrl-w X Remove current split
ctrl-w Q Remove all other splits
ctrl-w :resize Resize Screen (prompts for lines)

Sharing Interactive Sessions

Sharing Interactive Session

Just like in screeen, you can use tmux to share interactive sessions, which is great for code review or pair programming.

To share a tmux session, have a user login and create session storing socket in /tmp

$ tmux -S /tmp/da_socket

Set socket permissions:

$ chmod 777 /tmp/da_socket

The second user, logged in to the same machine, can now connect to that session

$ tmux -S /tmp/da_socket attach

You are now in the same session, hack away.

Additional Tips

You can also type extended commands for tmux, the command ctrl-w : will enter in a command-line mode. You can then issue various commands such as:

# split window horizontally 50%
splitw -h -p 50

# split window vertically 80%/20%
splitw -v -p 20

Additional Resources

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>