使用/配置 tmux

# 发行版直接安装
sudo apt install tmux
  • 配置到默认bash里面,直接进bash就打开tmux

shell=/bin/bash是~/.bashrc/shell=/bin/zsh是~/.zshrc

# TMUX_PANE
if [[ -n "$TERM_PROGRAM" || -n "$TMUX" ]]; then
    command echo "Runing in tmux 🎉..."
else
    # 运行 ls 查看是否有活跃的窗口, 如果没有就开启
    FIRST_WINDOW=$(tmux ls|head -n 1|awk -F':' '{print $1}')
    if [[ -n "$FIRST_WINDOW" ]]; then
        command tmux attach -t $FIRST_WINDOW
    else
        command tmux
    fi
fi
# 自行备份原有配置
ls ~/.tmux.conf
# 下载
cd
rm -rf .tmux
git clone https://github.com/gpakosz/.tmux.git
ln -s .tmux/.tmux.conf
cp .tmux/.tmux.conf.local .
# 重新打开终端,明显能看到底部导航发生变化,就行github的图片一样
# 注意,如果看不到界面,请先把终端窗口调大,行数和列数太小也不能显示完全

快捷键

  • tmux默认的前置操作是Ctrl+b.所有的操作都必须以前置操作开始.例如,我们想要新建一个窗体,就需要先在键盘上摁下CTRL+b,松开后再摁下n键 概览

  • 按下快捷键C(Ctrl)+b之后,界面显示一个小键盘的图标,进入监听快捷键状态,再次按C+b小键盘消失,取消监听快捷键状态

# 修改前置按键, 在tmux嵌套的时候可以使用; 默认嵌套是ctrl+b按两次就进入嵌套了
vim ~/.tmux.conf
# 使用Ctrl+n
set -g prefix C-n

基本操作

  • C+b ? 显示所有快捷键

  • C+b c new-session 创建一个新的会话

  • C-b d detach当前会话,可暂时返回Shell界面,输入tmux attach能够重新进入之前会话

  • C-b s 选择并切换会话;在同时开启了多个会话时使用

Session操作

# 创建一个新的session
$ tmux new -s test
# 进入session
$ tmux attach -t test
  • C-b s 列出所有会话

  • C-b d detach当前session(可以认为后台运行)

Window操作

  • C-b c 创建一个新窗口

  • C-b & 关闭当前窗口

  • C-b w 列出所有的窗口选择

  • C-b p 切换到上一个窗口

  • C-b n 切换到下一个窗口

  • C-b 窗口号 使用窗口号切换窗口(例如窗口号为1的, 则C-b 1)

  • C-b , 重命名当前窗口,便于识别各个窗口

Pane操作

  • C-b % 横向分Terminal

  • C-b " 纵向分Terminal

  • C-b 方向键 则会在自由选择各面板

  • C-b x 关闭当前pane

  • C-b q 显示面板编号

常见问题

  • sessions should be nested with care, unset $TMUX to force

# 销毁$TMUX
unset TMUX
# 重开tmux
tmux
  • 乱码

# 进入bash
bash
# 重进tmux
tmux attach -d
  • 杀死所有tmux进程

sudo kill -9 `ps aux| grep 'tmux' | awk '{print $2}'`
  • 图形化linux推荐使用tilix终端(tilix)[https://github.com/gnunn1/tilix/]

sudo apt install tilix
# 按照github文档配置即可
  • 其次ConEmuPack图形化终端软件,比cmder好用

# 默认使用wsl里面的zsh,在启动/任务里面添加
set "PATH=%ConEmuBaseDirShort%\wsl;%PATH%" & %ConEmuBaseDirShort%\conemu-cyg-64.exe --wsl -cur_console:pm:/mnt -t bash -c zsh
  • tmux-powerline

git clone --depth=1 https://github.com/erikw/tmux-powerline.git ~/.tmux/tmux-powerline

# 编辑tmux的配置文件, 参考: https://github.com/tmux/tmux/blob/master/example_tmux.conf
code ~/.tmux.conf

set-option -g status on
set-option -g status-interval 2
set-option -g status-justify "centre"
set-option -g status-left-length 60
set-option -g status-right-length 90
set-option -g status-left "#(/home/jcleng/.tmux/tmux-powerline/powerline.sh left)"
set-option -g status-right "#(/home/jcleng/.tmux/tmux-powerline/powerline.sh right)"
set-hook -g session-created 'run-shell "/home/jcleng/.tmux/tmux-powerline/powerline.sh init"' # prettifies the window-status segments


tmux source ~/.tmux.conf

tmux插件使用tmux-plugins

git clone --depth=1 https://github.com/tmux-plugins/tpm ~/.tmux/plugins/tpm

code ~/.tmux.conf
# 修噶后生效
tmux source ~/.tmux.conf
tmux source-file ~/.tmux.conf
# 编辑内容

# 配置文件参考: https://github.com/tmux/tmux/blob/master/example_tmux.conf
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# 插件市场: https://github.com/tmux-plugins/list
# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'github_username/plugin_name#branch'
# set -g @plugin '[email protected]:user/plugin'
# set -g @plugin '[email protected]:user/plugin'

# 网速: https://github.com/tmux-plugins/tmux-net-speed
# run-shell ~/.tmux/plugins/tmux-net-speed/net_speed.tmux
set -g @plugin 'tmux-plugins/tmux-net-speed'
set -g status-right "%H:%M|#{net_speed}"
set -g status-left "WSL| "
set -g default-terminal "tmux-256color"
set -g window-status-current-style "underscore"
set -g status-left-style bg=#F92658
# 状态刷新率
set -g status-interval 1

set -g status-style bg=#272822
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
  • 插件安装,注意很多插件脚本头是#!/bin/bash -,需要改为自己的#!/run/current-system/sw/bin/bash -

# tmux-net-speed 流量展示,有可能不支持
git clone --depth=1 https://github.com/tmux-plugins/tmux-net-speed.git ~/.tmux/plugins/tmux-net-speed
# 配置到文件
set -g @plugin 'tmux-plugins/tmux-net-speed'
set -g status-right "#{net_speed}"
  • 流量计算脚本

RX_sum() {
	local ethn=$1
	local RX_pre=0
	if [[ -z "$ethn" ]]; then
		# 如果网卡接口空,就计算所有的
		for interface in /sys/class/net/*; do
			ethn=$(echo $(basename $interface) " ")
			this_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')
			RX_pre=$((${RX_pre} + ${this_pre}))
		done
	else
		RX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $2}')

	fi
	echo $RX_pre
}
# 获取网卡的下载流量总和
TX_sum() {
	local ethn=$1
	local TX_pre=0
	if [[ -z "$ethn" ]]; then
		# 如果网卡接口空,就计算所有的
		for interface in /sys/class/net/*; do
			ethn=$(echo $(basename $interface) " ")
			this_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
			TX_pre=$((${TX_pre} + ${this_pre}))
		done
	else
		TX_pre=$(cat /proc/net/dev | grep $ethn | sed 's/:/ /g' | awk '{print $10}')
	fi
	echo $TX_pre
}
# 获取流量
speed() {
	local ethn=$(get_tmux_option @net_speed_interfaces "ens3")
	ethn='ens3'

	RX_pre=$(RX_sum $ethn)
	TX_pre=$(TX_sum $ethn)

	# echo RX_pre:$RX_pre
	# echo RX_pre:$TX_pre

	sleep 1
	RX_next=$(RX_sum $ethn)
	TX_next=$(TX_sum $ethn)

	if [[ -z "$RX_pre" ]]; then
		printf "数据获取空"
		exit 0
	fi
	# clear
	# echo -e "\t RX $(date +%k:%M:%S) TX"

	RX=$((${RX_next} - ${RX_pre}))
	TX=$((${TX_next} - ${TX_pre}))

	if [[ $RX -lt 1024 ]]; then
		RX="${RX}B/s"
	elif [[ $RX -gt 1048576 ]]; then
		RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
	else
		RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
	fi

	if [[ $TX -lt 1024 ]]; then
		TX="${TX}B/s"
	elif [[ $TX -gt 1048576 ]]; then
		TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
	else
		TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
	fi
	echo "$RX,$TX "
}
# echo 下载流量pre:$(RX_sum ens3)
# echo 上传流量pre:$(TX_sum ens3)
echo $(speed)
  • 常见指令

# 当前配置
tmux show-options -g
# 当前设置的状态数据
tmux show-options -g | grep status

# 临时设置
tmux set-option -gq "status-right-length" 100

# 永久设置修改~/.tmux.conf文件,让配置生效
tmux source-file ~/.tmux.conf
  • 我的配置

# 配置文件参考: https://github.com/tmux/tmux/blob/master/example_tmux.conf
set-option -g default-shell "/run/current-system/sw/bin/bash"
set-option -g default-command "/run/current-system/sw/bin/bash --init-file ~/.bashrc"
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'

# 插件市场: https://github.com/tmux-plugins/list
# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'github_username/plugin_name#branch'
# set -g @plugin '[email protected]:user/plugin'
# set -g @plugin '[email protected]:user/plugin'

# 网速: https://github.com/tmux-plugins/tmux-net-speed
# run-shell ~/.tmux/plugins/tmux-net-speed/net_speed.tmux
set -g @plugin 'tmux-plugins/tmux-net-speed'
set -g @plugin 'tmux-plugins/tmux-cpu'
set -g @plugin 'tmux-plugins/tmux-battery'
set -g status-right "CPU:#{cpu_percentage}|RAM:#{ram_percentage}|%m/%d %H:%M:%S|#{net_speed} #{battery_percentage}"
set -g status-left "ThinkPad| "
set -g default-terminal "tmux-256color"
set -g window-status-current-style "underscore"
set -g status-left-style bg=#F92658
# 状态刷新率
set -g status-interval 1
set -g "status-right-length" 100

set -g status-style bg=#272822
# 鼠标支持,点击移动选择等
set -g mouse on
# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.tmux/plugins/tpm/tpm'
# 临时配置可以加上tmux前缀, 写到配置文件无需前缀
# 当前配置列表
tmux show-options -g
tmux show-options -g | grep status

tmux set -g default-terminal "screen-256color"
# 刷新率, 秒
set -g status-interval 1

# 状态栏开关
set -g status on/off

# 状态栏背景
set -g status-bg "#272822"
# 状态栏前景字体颜色
set -g status-fg white

# 增加左边栏设置文本 status-left/status-right
set -g status-left "local_tmux| "
set -g status-left-length 120

# 设置时间
set -g status-right "%Y-%m-%d %H:%M:%S"