A good[ish] website
Web development blog, loads of UI and JavaScript topics
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:
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
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:
Here’s a list of the session related commands and key commands:
Command | Aliases | Action |
---|---|---|
tmux attach | a , at , attach-session | Attach to last session |
tmux attach -t myses | ditto | Attach to myses |
tmux ls | list-sessions , Ctrl+b s | List session |
tmux | new , new-session , :new | New session |
tmux new -s myses | :new -s myses | New named session |
tmux kill-ses -t myses | kill-session | Kill myses |
tmux kill-ses -a | ditto | Kill all but current |
tmux kill-ses -a -t myses | ditto | Kill all but myses |
Ctrl+b $ | - | Rename session |
Ctrl+b d | - | Detach session |
Ctrl+b ( | - | Previous session |
Ctrl+b ) | - | Next session |
Tmux windows are analogous to tabs.
Here’s all the key combos:
Key | Action |
---|---|
Ctrl+b c | Create window |
Ctrl+b , | Rename current window |
Ctrl+b & | Close current window |
Ctrl+b p | Previous window |
Ctrl+b n | Next window |
Ctrl+b 0...9 | Select window by number |
:swap-window -s 2 -t 1 | Reorder window |
:swap-window -t -1 | Move current window left one position |
With panes you can split your screen into two or more parts.
Key | Action |
---|---|
Ctrl+b % | Horizontal split |
Ctrl+b " | Vertical split |
Ctrl+b o | Swap panes |
Ctrl+b q | Show pane numbers |
Ctrl+b x | Kill pane |
Ctrl+b ⍽ | Space - toggle between layouts |
So, there’s three tiers of copying text that you’ll face in 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.
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.
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.