Tmux

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Tmux and the translation is 61% complete.
Outdated translations are marked like this.
Other languages:
Resources

tmux (terminal multiplexer) 是一款终端复用类程序,它允许在单个终端(或窗口)中,管理多个分离的会话,窗口及面板。tmux 可以从屏幕上分离并继续在后台运行,然后重新连接。[1] 熟悉GNU Screen 的用户可能会发现tmux是另一个很好的选择。

Users familiar with GNU Screen may find tmux as a suitable alternative.

安装

USE 标记

USE flags for app-misc/tmux Terminal multiplexer

debug Enable extra debug codepaths, like asserts and extra output. If you want to get meaningful backtraces see https://wiki.gentoo.org/wiki/Project:Quality_Assurance/Backtraces
selinux !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
systemd Enable use of systemd-specific libraries and features like socket activation or session tracking
utempter Include libutempter support
vim-syntax Pulls in related vim syntax scripts

Emerge

安装 app-misc/tmux:

root #emerge --ask app-misc/tmux

配置

标准方式安装tmux不会安装全局(/etc/tmux.conf)或用户(~/.tmux.conf)配置文件。示例在 {{Path|/usr/share/doc/tmux-{VERSION}/example_tmux.conf}} 文件中提供,可以复制到全局位置或用户位置,并使用文本编辑器根据需要进行编辑。

Configuration Files

  • /etc/tmux.conf — the system-wide configuration file.
  • $XDG_CONFIG_HOME/.config/tmux/tmux.conf — The user specific tmux configuration file.
  • ~/.tmux.conf — The legacy single-user tmux configuration file location.


Wiki 示例

文件 ~/.tmux.conf
set -g default-terminal "screen-256color"   # Use 256 colors
set -g history-limit 100000                 # Scrollback buffer number of lines
 
# 窗口序号从1开始,而不是0。set -g base-index 1
 
# 设置前缀键和一些键绑定以匹配GNU屏幕
set -g prefix C-a
bind-key C-a last-window
 
# 水平和垂直窗格的按键绑定
unbind %
bind | split-window -h
bind - split-window -v
 
# 启用窗口标题
#set -g set-titles on
 
# 窗口标题字符串(使用状态栏变量)
set -g set-titles-string '#T'
 
# 带负载和时间的状态栏
set -g status-bg '#4e4e4e'
set -g status-fg '#ffffff'
set -g status-left ' '
set -g status-right '#[bg=#bbbbbb]#[fg=#000000]#(cut -d " " -f -3 /proc/loadavg) #[bg=#4e4e4e]#[fg=#ffffff]#(
date +" %H:%M ")'
set -g window-status-format '#I #W'
set -g window-status-current-format ' #I #W '
setw -g window-status-current-bg '#55ff55'
setw -g window-status-current-fg '#000000'
 
# 窗格边框颜色
set -g pane-active-border-fg '#bbbbbb'
set -g pane-border-fg '#555555'
 
# 绑定以重新加载配置
bind r source-file ~/.tmux.conf
  1. Match session numbers to number row

set -g base-index 1

  1. Set TERM, the default is "screen", "screen-256color" can be tried if "tmux-256color" doesn't work.

set -g default-terminal "tmux-256color"

  1. Set prefix to 'a' with ctl-a > a to send ctrl-a to the terminal
  2. set -g prefix C-a
  3. bind-key a send-prefix
  4. Set prefix2 to the default 'b'
  5. set -g prefix2 C-b
  6. Enable mouse

set -g mouse on

  1. Disable drag action in normal mode if your pointer causes problems with

unbind -n MouseDrag1Pane

  1. Also disable in copy mode
  2. unbind -Tcopy-mode MouseDrag1Pane
  1. Set the bar and inactive borders to blue, highlighting current in green.
  2. set-option -g status-style fg=black,bg=blue
  3. set-window-option -g window-status-current-style bg=green
  4. set-option -g pane-border-style fg=blue
  5. set-option -g pane-active-border-style fg=green

}}

To reload the configuration file from a terminal run:

user $tmux source-file ~/.tmux.conf

Alternatively, modifications to the file can be loaded from within tmux via:

:source-file ~/.tmux.conf

Automatic connection

Running tmux with exec and the -ADX options will cause tmux to replace the current shell and create a session, or if one exists connect to it and both detach and exit the other client. This provides a very consistent way of working with remote sessions, and it can be run automatically:

Bash

user $test -n "$PS1" && test -z "$TMUX" -a -n "$SSH_TTY" && exec tmux new -ADX

Fish

user $status is-interactive ; and test -z "$TMUX" -a -n "$SSH_TTY" ; and exec tmux new -ADX

插件

tmux可以使用几个plugins。 请参阅以下各节以了解可用选项。

tpm

tpm is a tmux plugin manager. See the tpm sub-article for more details on the installation process.

tmux-mem-cpu-load

tmux-mem-cpu-load是一个小程序,旨在监视tmux状态行中的系统活动。有关安装过程的更多详细信息,请参见tmux-mem-cpu-load子文章。

Tmux 复活

tmux-resurrect 在系统重启时保持 tmux 环境。有关安装过程的更多详细信息,请参阅 resurrect 子文章。

使用

按键绑定

tmux 可以通过使用“前缀键击”(默认为Ctrl+b)后跟“命令”按键的组合键方式从已连接的客户端进行控制。

按下 Ctrl+B 后,可以使用以下组合键:

常规

  • ? = 列出所有绑定按键。
  • d = 分离当前客户端。
  • : = 进入 tmux 命令行模式。

创建和管理窗口

  • c = 创建一个新窗口。
  • n = 切换到下一个窗口。
  • p = 切换到上一个窗口。
  • l = 切换至先前选择的窗口。
  • 0-9 = 切换窗口 0 到 9。
  • ' = 提示选择窗口索引。然后输入数字或标题以切换到该窗口。
  • , = 重命名当前窗口。
  • w = 以交互方式选择当前窗口。
  • :,然后键入list-windows enter = 显示窗口列表。

创建和管理窗格

  • " = 将当前窗格拆分为上下两部分。
  • % = 将当前窗格拆分为左右两部分。
  • o = 选择当前窗口中的下一个窗格。
  • ; = 移至先前活动的窗格。
  • { = 将当前窗格与前一个窗格交换。
  • } = 将当前窗格与下一个窗格交换。
  • Ctrl+o = 顺时针旋转当前窗口中的窗格。
  • Alt+1 to Alt+5 = 以五种预设布局之一排列窗格:均匀水平、均匀垂直、主水平、主垂直或平铺。
  • x = 关闭当前窗格。
  • ! = 将当前窗格从窗口中分离出来。

复制、粘贴和滚动操作

可用按键取决于选择的是 emacs(默认)还是 vi 模式。可以通过 .tmux.conf 中 mode-keys 选项设置 vi 模式。

  • [ = 进入复制模式以复制文本或查看历史记录。
  • ] = 粘贴上一次复制的缓冲区文本。
  • # = 列出所有粘贴缓冲区。
  • - = 删除上一次复制的缓冲区文本。

会话控制

开始会话

启动 tmux 后将在 /tmp/S-<UID>/<Session Name> 中为会话创建一个套接字

tmux 可以使用以下命令启动:

user $tmux

或者,要在启动时为会话命名,请执行:

user $tmux new-session -s portage

列出会话

列出 tmux 会话以查看现有会话信息:

user $tmux ls
0: 1 windows (created Thu Apr  9 09:09:03 2015) [180x65] (attached)

列出会话时,会话名称将显示为会话信息行中的第一项。从上面的输出中可以看出,会话是在未命名的情况下创建的,因此会话将被引用为 0

另一种列出会话的方法是输入长命令 list-sessions 参数。

user $tmux list-sessions
0: 1 windows (created Thu Apr  9 09:09:03 2015) [180x65] (attached)

显示与上一个命令完全相同的输出。

重命名会话

简单地使用 tmux 创建会话不会为其提供一个漂亮的、人类可读的名称。

如果默认会话名称的描述性不够(0 描述过少),则可以重命名会话。假设 Larry 在启动时没有指定会话名称就启动了 tmux。他开始着手编译新版本的 Portage,并希望更改会话名称以反映他当前的任务。要更改会话名称,他首先通过魔术键:Ctrl+b来控制 tmux,然后键入 :将焦点移动至 tmux 控制行。默认情况下,该线应变为黄色。接下来,他将输入:

:rename-session -t 0 portage

其中 0 是现有(默认)会话名称,portage 是会话想要的新名称。可在 tmux 会话分离的情形下重命名:

user $tmux rename-session -t 0 portage

恢复会话

会话分离后,所有活动终端仍保持活动状态,尚未完成的命令也是如此。要恢复会话,请使用 attach -t <session_name>

user $tmux a -t portage

或者使用长方式来恢复现有会话:

user $tmux attach -t portage

类似守护进程的操作

要在 tmux 会话中“不”连接到会话(如守护进程)以执行命令,请使用 new-session -d 后跟用引号包裹的要执行的命令:

user $tmux new-session -d 'emerge -uDNvp @world'

另请参阅

外部资源

Tutorial Videos

参考