Tmux Notes

By Published On: January 23, 20204.3 min readViews: 197

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

  1. Check all configuration: tmux show-options -g
  2. Reload conf in a session: source-file <tmux.conf>
  3. 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

  1. Pre [ to enter copy mode.
  2. 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 press Enter . Press n to search for the same string again. Press Shift-n for reverse search as in emacs mode. Press q twice to exit copy 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:

  1. tmux-plugins/tpm
  2. tmux-plugins/tmux-sensible
  3. tmux-plugins/tmux-resurrect
    • Save an entire tmux session: prefix + Control + s
    • Restore an entire tmux session: prefix + Control + r

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.

  1. In iTerm2, General -> Command:
  tmux attach -t base || tmux new -s base
  1. In tmux, Prefix (Control + B) + w, it will list all windows. Each entry it has a shortcut. You might notice that, after 0~9 , it started with M, like M-i. The M means Meta Key. In iTerm2, you need to set it. I’ve set it as below:

 

My customization

  1. 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
  1. Navigating between panes (NOT windows): Prefix + (→ | ← | ↑ | ↓)
  2. Find the mouse mode setting in ~/.tmux.conf.local and uncomment it out as below:
# start with mouse mode enabled
set -g mouse on
  1. 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!

Share it:

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments