A good[ish] website

Web development blog, loads of UI and JavaScript topics

Master tmux in just... like 20 minutes or so

Filed under: Tooling— Tagged with: tmux, shell, homebrew

I’m going to teach myself to use tmux and will document it here.

Tmux stands for Terminal Multiplexer, it’s basically the same as screen but newer and has a shorter man page. Roughly speaking, it has two main functions:

  1. Start a new tmux session where you leave a long lasting command running, like an Irssi IRC client for example, then you can close the terminal connection but tmux keeps the process alive in the background. When you come back later on, you attach to the session.
  2. Split the terminal into panes and windows for your viewing comfort.

Install tmux

On Mac:

$ brew install tmux

At the time of writing this, it got me the version 2.2, but it’s not always the latest. See the tmux version in Github. You can get the freshest one like so:

$ brew install --HEAD tmux

Install in Debian based system:

$ sudo apt install tmux

Tmux sessions

First, start a new tmux session:

$ tmux

That will start an unnamed tmux session. You can name it as you start it:

$ tmux new-session -s mySessionName

Now you’re at the tmux session, you’ve got your shell there like you had before. Press ctrl+b and now you can hit a key that does something. For example:

  1. Hit ctrl+b and then c, a new window opens into the current session.
  2. Hit ctrl+b c again and you’ll have 3 windows open.
  3. Hit ctrl+b again and move to next windows with n or to the previous with p, or by hitting a number from 0 to 2.
  4. You can kill the session with ctrl+b &.

Here’s a list of the session related commands and key commands:

tmux attacha, at, attach-sessionAttach to last session
tmux attach -t mysesdittoAttach to myses
tmux lslist-sessions, Ctrl+b sList session
tmuxnew, new-session, :newNew session
tmux new -s myses:new -s mysesNew named session
tmux kill-ses -t myseskill-sessionKill myses
tmux kill-ses -adittoKill all but current
tmux kill-ses -a -t mysesdittoKill all but myses
Ctrl+b $-Rename session
Ctrl+b d-Detach session
Ctrl+b (-Previous session
Ctrl+b )-Next session

Tmux windows

Tmux windows are analogous to tabs.

Here’s all the key combos:

Ctrl+b cCreate window
Ctrl+b ,Rename current window
Ctrl+b &Close current window
Ctrl+b pPrevious window
Ctrl+b nNext window
Ctrl+b 0...9Select window by number
:swap-window -s 2 -t 1Reorder window
:swap-window -t -1Move current window left one position


With panes you can split your screen into two or more parts.

Ctrl+b %Horizontal split
Ctrl+b "Vertical split
Ctrl+b oSwap panes
Ctrl+b qShow pane numbers
Ctrl+b xKill pane
Ctrl+b ⍽Space - toggle between layouts

Copy mode

So, there’s three tiers of copying text that you’ll face in tmux:

  1. Copy inside tmux.
  2. Copy to your system’s clip board.
  3. Copy to your host computers clipboard over an ssh connection.

Copy text from the tmux buffer

  1. Enter the copy mode with ctrl+b [.
  2. Move to the spot you want to start copying and press ctrl+space.
  3. "Paint" the text with your arrow keys you want to copy.
  4. Press ctrl+w or alt+w (or enter in some systems) to copy into Tmux buffer.
  5. Press ctrl+b to paste somewhere else inside tmux.

Copy text to the system clipboard from tmux

By default, if you copy anything inside tmux, it will copy to the tmux buffer, not to your system’s clipboard, so you can only paste inside the tmux session, but not into another application.

The following should work on tmux version 2.4 and newer.

Put this in your .tmux.conf:

bind -T copy-mode-vi v send -X begin-selection

Does it now copy to the clipboard? No? Sometimes this has worked for me, and sometimes not. If it doesn’t work, you can install reattach-to-user-namespace, which is in homebrew:

$ brew install reattach-to-user-namespace

The add the needed config to .tmux.conf:

bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"

See this post about copying in tmux, it’s pretty good.

There’s also a way to use xclip to copy, but that needs xquartz installed.

How to install xlip on Mac:

Here’s xclip installation instructions that I tried earlier:

brew install xclip

It might fail and ask you to install xquartz, go ahead and do it: brew cask install xquartz. On Mac you also have natively pbcopy, but at the time mine just didn’t work so I went with xclip and installed xquartz.

On a Debian based system:

sudo apt install xclip

Then add the following to your ~/.tmux.conf:

bind -t vi-copy y copy-pipe "xclip -sel clip -i"

Use copy as described above.

Configuring tmux

As mentioned earlier, by default the tmux config lives is ~/.tmux.conf.

After the config file has been updated, tmux needs to know about it:

$ tmux source-file ~/.tmux.conf

My config is pretty minimal. I like to set the mouse on, which enables: a click on a pane to activate it, size the panes by dragging, and text selection with the mouse. Also the active/inactive styles are pretty nice to have, I’ve got the inactive pane kind of grayed out. It’s also good to have a large buffer size when logging large object etc.

# Copy to system clipboard.
bind -T copy-mode-vi v send -X begin-selection
bind-key -T copy-mode-vi 'y' send -X copy-pipe-and-cancel "reattach-to-user-namespace pbcopy"

# Set fish as the shell.
set -g default-shell /usr/local/bin/fish

# Set larger buffer size.
set -g history-limit 10000

# Set inactive/active window styles
set -g window-style 'fg=colour247,bg=colour236'
set -g window-active-style 'fg=colour250,bg=black'

# Activate the mouse
set -g mouse on

Comments would go here, but the commenting system isn’t ready yet, sorry.

  • © 2022 Antti Hiljá
  • About
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!