tmux is a terminal multiplexer
Tmux is a terminal multiplexer, that’s how it gets its name: t(Terminal) mux(multiplexer), you can manage multiple terminals on one screen!
On a MAC, thanks to Iterm2, you can manage multiple terminal Windows quite easily. But on Linux, iterm2 is out of reach, and TMUx’s advantages come to the fore.
As I understand it, TMUX has many benefits on Linux or MAC
-
Split screen
Of course, iterm2 can do split-screen, but iterm2 has the disadvantage of iterm for Mac. If iterm2 is combined with SSH, the iterm2 split screen needs to be constantly SSH
, resulting in multiple users connected to the who command to check the number of login users.
-
attach
Attach can protect the site so that the working environment will not be lost due to SSH timeout.
-
The operation is simple and configurable
You can use shortcuts to quickly switch between Windows, panels, paste and copy, and scroll endlessly.
As for its beauty, it is reflected in the status bar with customizable styles. You can set the status bar style, position, style of the current window, and richness of the status bar information. Such as the configuration of gpakosz/.tmux
This chapter covers the following
- How to install on Contos/MAC
tmux
- Automate batch installation using Ansible
tmux
tmux
Common operations
- Window multiplexing with TMUx Github
- Series of articles: Server operation notes
The installation
If you are on a MAC, you can install TMUx easily and quickly using Brew Install TMUx.
However, if you use yum to install software on centos, you will install very old versions and many useful functions will not be used. It is a good idea to compile and install tMUX source code
#Installation Software Dependency
$ yum install -y gcc automake libevent-devel ncurses-devel glibc-static
#Download the source code
$ git clone [email protected]:tmux/tmux.git
#Switch to version 2.8
$Git checkout 2.8
$ cd tmux
#Compile the source code
$ sh autogen.sh && ./configure && make
#Viewing the Version Number
$ ./tmux -VTmux 2.8Copy the code
Automate the installation using Ansible
When compiling and installing from source code is trivial and error-prone, and you may need to install TMUx on several servers, an automated installation using Ansible is a good choice. See this series of articles about Using Ansible for automated o&M.
$ git clone [email protected]:shfshanyue/ansible-op.git
#Install TMUX on multiple servers at once
$ ansible-playbook -i hosts tmux.yml
Copy the code
Tmux This ansible role configuration is on my TMUX configuration. The configuration file is as follows
Install dependencies
- name: prepare
yum:
name: "{{item}}"
with_items:
- gcc
- automake
- libevent-devel
# character terminal processing library
- ncurses-devel
- glibc-static
Download the TMUX source file
- name: install tmux
git:
repo: https://github.com/tmux/tmux.git
dest: ~/Documents/tmux
version: 2.8
- name: make tmux
shell: sh autogen.sh && ./configure && make
args:
chdir: ~/Documents/tmux/
# make TMUx globally executable
- name: copy tmux
copy:
src: ~/Documents/tmux/tmux
dest: /usr/bin/tmux
remote_src: yes
mode: 0755
# Use my profile
- name: clone config file
when: USE_ME
git:
repo: https://github.com/shfshanyue/tmux-config.git
dest: ~/Documents/tmux-config
- name: copy config file
copy:
src: ~/Documents/tmux-config/.tmux.conf
dest: ~/.tmux.conf
remote_src: yes
- name: delete tmux-config
file:
name: ~/Documents/tmux-config
state: absent
Copy the code
Quick start
The term
server
Contains more than onesession
session
Contains more than onewindow
window
Similar to theiterm2
的Tab
, contains multiplepane
, the following Chinese will be translated as Windowspane
Similar to theiterm2
的Pane
, the following Chinese will be translated as panel
Start the
#Create a TMUX session
$ tmux
Copy the code
Split screen
Use the shortcut keys prefix % and prefix “to split the screen in tMUX
View all shortcut keys
prefix ?
Tmux high frequency operation
The prefix key is sent to the terminal
I used < crtl-s > as the prefix key, the default prefix is
. Send-prefix indicates that a prefix key is sent to the terminal. Send-prefix-2 indicates that a shortcut key is added to the prefix key.
#The following commands are directly executed in tmux command mode, or executed in shell with keyword 'tmux', or written to the configuration file ~/.tmux.conf to take effect
#'prefix:' can enter tMUx command mode
$ set -g prefix2 C-s
$ bind C-s send-prefix -2
Copy the code
Keeping SSH Connections
Every time a new session is created, bring the name for the convenience of attaching next time.
Just to mention the detach command, the default shortcut
Prefix defaults to < ctrl-b >
$ tmux new -s shanyue
#Or use the shortcut prefix + D
$ tmux detach
$ tmux attach -t shanyue
Copy the code
Fast-moving panel
The move panel command is select-pane, which can be configured as a Vim-style move command.
#The following commands are directly executed in tmux command mode, or executed in shell with keyword 'tmux', or written to the configuration file ~/.tmux.conf to take effect
#'prefix:' can enter tMUx command mode
# bindBind shortcut key
#-r The key can be repeated
#Select-pane Selects the panel
$ bind -r h select-pane -L
$ bind -r l select-pane -R
$ bind -r j select-pane -D
$ bind -r k select-pane -U
Copy the code
The -r parameter indicates the repeatable button. For example, prefix r r indicates prefix r, prefix r. The keystroke time needs to be set through repeat-time, which is generally 500ms.
You can also enable mouse support to move the panel quickly.
Rename the window name
Rename -window command to rename a window, default shortcut prefix,
But there is a small problem, every time after renaming the window name, typing a few Spaces will automatically rename, their hard work was destroyed…
You need to configure the following two configurations to turn them off and finally rename them
#The following commands are directly executed in tmux command mode, or executed in shell with keyword 'tmux', or written to the configuration file ~/.tmux.conf to take effect
#'prefix:' can enter tMUx command mode
set -wg allow-rename off
set -wg automatic-rename off
Copy the code
Enabling mouse Support
$ tmux set -g mouse on
Copy the code
Mouse support is disabled by default. After the mouse is enabled, you can copy, flip screen, switch panel, switch window, and resize.
The mouse support is powerful, and so far an Iterm2 has been successfully built. But I don’t like the mouse mode, so it’s banned.
Besides, after opening the mouse support, who can operate my terminal, a bit forced case also did not have
Save the current path
When pane and Window are new, keep the current path. Add the -c parameter to the previous command to indicate the path to a new window or panel.
The command to open a new panel is split-window
#The following commands are directly executed in tmux command mode, or executed in shell with keyword 'tmux', or written to the configuration file ~/.tmux.conf to take effect
#'prefix:' can enter tMUx command mode
bind c new-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind '"' split-window -c "#{pane_current_path}"
Copy the code
Maximize the current panel
The command is tmux resize-pane-z, and the default shortcut is prefix Z. To view more detailed information, press Prefix Z to enter the full screen. After that, press Resume again. Pretty cool feature.
Turn over the screen
The first time you use TMUx, with Webpack, the output is a lot and useful error messages are overwritten. At this point, scrolling up is very important. Now let’s talk about the two modes in TMUx Window,
-
default-mode
It’s just entered the tMUX default mode.
-
copy-mode
Press prefix to enter this mode, which is similar to normal mode vi(Emacs) and supports copy, paste, search, and page turning. The default mode is vi. If not, set it to vi. If not, set it to vi
$ tmux show-window-options -g mode-keys Copy the code
Same as vi, you can use C-D and C-u for page up and down (half screen), but you can also use HJKL.
Alternatively, you can turn on mouse support and use the scroll wheel to scroll through the screen.
Copy and paste
So copy-mode, copy and paste. After entering copy-mode, v will be selected and Y will copy and exit copy-mode. Use prefix] to paste.
Copy and paste is the most important and useful feature in Linux
V & Y is a user-defined configuration. The configuration is as follows
bind -t vi-copy v begin-selection
bind -t vi-copy y copy-selection
Copy the code
The copy operation stores the contents into buffer. You can become more familiar with the following commands to manipulate buffer
$ tmux list-buffers List all buffers
$ tmux show-buffer -b [name] The latest buffer is displayed
$tmux choose-buffer     Enter the buffer selection screen, more flexible
Copy the code
Alternatively, you can turn on mouse support and use the mouse to select text.
Find keywords
Now that you are in copy-mode, those familiar with vi must know that the search is/and? .
Quick location window
Suppose you create multiple Windows and need to locate a window quickly. You know what’s in that window, but you can’t remember the window number.
A good solution is the find-window command, and even better is the default prefix F shortcut. Enter the keyword of the window content, you can quickly locate the window, but there is a small disadvantage, it is unable to locate the panel!
Last but not least
Man tmux! ** do not see the document is not skilled, do not see the source code is not proficient. ** As a result, you need to read more documents and commands.
summary
In this chapter you will become very comfortable with using TMUx to manage multiple Windows at the same time on a server, and with vim you will be able to do whatever you want on a server. For viM usage, configuration, and plugins, see the previous chapter viM Quick Start, Configuration, and Plugins.