Hi there!
Today I'd like to share you my notes about tmux!
Tmux is my favorite terminal multiplexer! Several years ago I didn't give a **it for people using it! Because I thought that might consume too much of my time to customize. However, one day I was free, then tested the water! I feel like I couldn't live without it in my coding environment!
It likes Vim, the learning curve is steep, but once you're comfortable with it, you will addict to it!
No more talking, let's dive into it!
Introduction
It’s tmux, a so-called terminal multiplexer. Simply speaking, tmux acts as a window manager within your terminal 1 and allows you to create multiple windows and panes within a single terminal window.
Pane
Shortcut | Comment |
---|---|
Pre % |
Splitting panes in left and right |
Pre " |
Splitting panes in top and bottom |
Pre <arrow key> |
Navigating in panes |
C-d |
Close panes |
Pre: swap-pane -s <sid> -t <tid> |
Swap sid pane to tid pane |
Pre z |
Make a pane go full screen, vice versa |
Pre C-<arrow key> Pre ⌥-<arrow key> |
Resize pane in direction of |
Windows
Shortcut | Comment |
---|---|
Pre c |
Create a new window |
Pre , |
Rename current window |
Pre x |
Close current window with prompt and deattach |
Sessions
Shortcut | Comment |
---|---|
Pre :new -s <name> |
Create a new session |
Pre C-c |
Create a new session |
Pre $ |
Rename current session |
Pre s, then x on the session |
Delete the selected session |
Configuration
This is the folder of configuration: ~/.tmux
.
This is the configuratino file: ~/.tmux.conf
- Check all configuration:
tmux show-options -g
- Reload conf in a session:
source-file <tmux.conf>
- Reload conf out of a session:
tmux source-file <.tmux.conf>
Session Handling
tmux ls (Same as: tmux list-sessions) tmux kill-server (You can think this is to kill/remove all sessions) tmux attach -t 0 tmux rename-session -t 0 <new session name> tmux new -s <session name> tmux attach -t <session name> tmux rename-session -t <old session name> <new session name> tmux kill-session -t targetSession (kill the specific session)
Search
- Pre
[
to entercopy mode
. - If you're using
vi
key bindings (Ctrl-b:set-window-option -g mode-keys vi
), press/
then type the string to search for and pressEnter
. Pressn
to search for the same string again. PressShift-n
for reverse search as in emacs mode. Pressq
twice to exitcopy mode
. You can use?
to search in the reverse direction.
Pluggins
I haven't explored much in pluggins, but this is the ones I used or come with tmux by default, you can give a try:
- tmux-plugins/tpm
- tmux-plugins/tmux-sensible
- tmux-plugins/tmux-resurrect
- Save an entire tmux session:
prefix + Control + s
- Restore an entire tmux session:
prefix + Control + r
- Save an entire tmux session:
Use conf from Github
I used https://github.com/gpakosz/.tmux.git. It's well customized.
My .tmux.conf
and .tmux.conf.local
are based on it.
Integrate with iTerm2
I have iTerm2 installed on my Mac, so I want to integrate tmux with it.
- In iTerm2,
General -> Command
:
tmux attach -t base || tmux new -s base
- In tmux,
Prefix (Control + B) + w
, it will list all windows. Each entry it has a shortcut. You might notice that, after0~9
, it started withM
, likeM-i
. TheM
meansMeta Key
. In iTerm2, you need to set it. I've set it as below:
My customization
- Moving windows by adding below settings into
~/.tmux.conf.local
(C
means Control,S
means Shift, Left/Right means arrow keys):
bind-key -n C-S-Left swap-window -t -1; select-window -t -1 bind-key -n C-S-Right swap-window -t +1; select-window -t +1
- Navigating between panes (NOT windows):
Prefix + (→ | ← | ↑ | ↓)
- Find the mouse mode setting in
~/.tmux.conf.local
and uncomment it out as below:
# start with mouse mode enabled set -g mouse on
- The script to attach or create tmux session. You can configure it at the startup of iterm2. The script will prevent attaching to the tmux session again if it has already been attached in another iTerm2 tab. The reason why I'd like to have this script is that, if not prohibit it, you will see exact same tmux session in your all new iTerm2 tab... then what's the purpose of creating new iTerm2 tab.
#!/bin/zsh tmux ls|grep kongfu|grep -q attached if [[ $? != 0 ]] ; then tmux attach -t kongfu || tmux new-session -s kongfu else echo "********************************************************************************" echo "* Ignore attaching tmux kongfu session as it has been attached already. *" echo "********************************************************************************" fi
Misc
Resolution problem in multiple monitors
Let’s say you’re connecting to a remote server over ssh with Terminal.app. When you “tmux attach” with bigger resolution monitor from smaller one you previously started tmux, it draws dots around the console. It doesn’t fit the new window size.
Resolution: You can always choose which client you want to detach from the session by pressing: C-b D
Okay, that's all for us today!
Hope you love it!