Today’s post is going to be short and sweet. You’re probably used to SSH’ing into remote instances and doing some debugging, running some commands, etc. Usually a plain terminal is fine for this. But sometimes you’re doing something that requires a long lived process and you don’t want the process to be killed if you happen to disconnect from the remote server. Or maybe you just want a sweet looking terminal. Anyway, this is where tmux (short for terminal-multiplexer) comes in.

First things first, here’s the tmux page, and here’s the inspiration for this post. Fair warning: this post is going to be a much shorter version of Ham’s blog post (maybe in the future I’ll edit if I discover any additional usage patterns for tmux I like).

In short, tmux lets you create “sessions” that are basically individual processes running in their own “pane”. You can create, attach/detach, and delete sessions. As long as the tmux server stays running (and you don’t explicitly delete something), then the processes running in your various sessions won’t be affected if your connection happens to drop. This is pretty handy, but it’s easy to forget the keyboard shortcuts for the common tmux operations, so I figured I’d list them here.

First you need to install tmux. Do the usual sudo apt-get install tmux or brew install tmux. Next, a summary of the notation: C-b (for instance) means “press Ctrl and b at the same time”. At the risk of stating the obvious, the $ in the lines below are the terminal prompt.

Commands and shortcuts:

  • $ tmux: start a new session
  • $ tmux ls: list running sessions (useful for getting session identifiers)
  • $ tmux new -s my-session-name: create a named session
  • $ tmux rename-session -t [number] new-name: rename a session
  • $ tmux attach -t [number]: attach to a session
  • $ C-b %: splits pane into 2 (left-right)
  • $ C-b ": splits pane into 2 (top-bottom)
  • $ C-b [arrow key]: switch to the pane indicated by the arrow key
  • $ exit or C-d: kill the pane; exiting all panes will kill the session
  • $ C-b c: create a new window
  • $ C-b [p|n] or $ C-b [number]: switch to the (p)revious or (n)ext window or window number
  • $ C-b d: to detatch from a session

That’s pretty much all you need to get over the initial hurdle. As a followup, here’s a link to Ham’s post about customizing your tmux config.