How to use Tmux and personalize configuration

Source: Internet
Author: User
Tags clear screen time 0 ssh access

#介绍
Tmux is an excellent terminal reuse software, even if the non-normal drop line, but also to ensure that the current task runs, this is particularly useful for remote SSH access, the network is not a bad situation can still ensure that the work site is not lost! In addition, Tmux fully uses the Keyboard control window to achieve the window switching function.
To put it simply, Tmux has two main functions for me (this should also be the main function of Tmux):
Split window. You can open multiple terminals under one terminal, or you can split the current screen in a variety of ways, so that you can open multiple terminals with smaller display ranges at the same time.
In the use of SSH environment, to avoid network instability, resulting in the loss of the work site. Imagine the following scenario when you execute a command and the SSH connection is broken due to network instability. At this point, you don't know if the previous command was successful. If you open a lot of files at this time, into a deeper directory, due to network instability, SSH connection disconnected. After reconnecting, you have to reopen those files and enter that deep directory. If the use of Tmux, reconnect later, you can directly back to the original work environment, not only improve the efficiency, but also reduce the risk, increased security.
#安装
sudo apt-get install Tmux
After the installation is completed, enter the command Tmux to open the software, the interface is very simple, similar to a terminal console with the status bar below; No accident, this time you will be the same as I did, think Tmux nothing.
#会话, window, panel
Based on the definition of Tmux, when the Tmux server is turned on, a session is created first, and the session first creates a window that contains only one panel, that is, the so-called terminal console should be called a panel of Tmux, although it is used exactly the same way as the terminal console.
Tmux is constructed using the C/S model, which consists of the following modules:
Server servers. A server is turned on when you enter the Tmux command.
Session sessions. A server can contain multiple sessions
window. A session can contain multiple windows.
Pane panel. A window can contain multiple panels.
#初体验
Let's take a look first, then learn TMUX configuration and use I generally use this, because my notebook is only 13 inches:-)
Tmux-small
Of course, as a quasi it man, I also have a big screen, so I use it occasionally:
Tmux-large
All you see is a session in X session and a window in the Y window below that session. Yes, you can have so many terminals in one window, which is the main function of Tmux. OK, here is the tmux of the common keys, you first play a bit and then look down.
#常用按键
It is important to note that any instruction in Tmux contains a prefix, that is, after you press the prefix (a set of keys, the default is Ctrl+b), the system will know that your next instruction is sent to Tmux.
C-b? Show shortcut keys Help
C-b c-o Swap window position, similar to the c-w in vim
C-b space key with next built-in layout
C-b! Change the current window to a new window
C-b "Horizontal Divider window
C-b% Vertical Divider window
C-b Q Displays the number of the separator window
C-b o jumps to the next separator window
C-b the previous and next separated windows of the upper and lower keys
C-b C-direction key to resize a delimited window
C-b c Creating a new window
C-b 0~9 Select the window
C-b c Creating a new window
C-b n Select the next window
C-b L switch to the last used window
C-b P Select Previous window
C-b w display and select window in menu mode
C-b t Display clock
C-b; Switch to the last used panel
C-b x Close Panel
C-b & Close Window
C-b s display and select sessions in menu mode
C-b D exits tumx and saves the current session, at which point the Tmux is still running in the background and can be entered into the specified session via Tmux attach
#配置
Let's take a look at a few configurations, which is why I can't live without tmux:-). Tmux configuration file is ~/.tmux.conf, this file may not exist, you can create your own. The following start configuration, first, there is no sense that the prefix tmux is too inconvenient, CTRL and B key separated too far, many people to map it into C+a, that is, in the configuration file (~/.tmux.conf) to add the following statement:
#设置前缀为Ctrl + A
SET-G prefix c-a
At the same time, cancel the default prefix key:
The correspondence between #解除Ctrl +b and prefixes
Unbind C-b
After configuration, restart Tmux, or press c+b first, then enter:, enter command line mode, in command line mode input:
Source-file ~/.tmux.conf
You can also be the same as me, in the configuration file to add the following sentence, later changed the only need to press the prefix +r.
The #将r is set to load the configuration file and displays "reloaded!" Information
Bind r source-file ~/.tmux.conf \; Display "reloaded!"
About the prefix, many people like to change to Ctrl + A, but I personally prefer ctrl+x, if you are a vim user, you must understand. There is the panel switch is not convenient, need to first press the prefix, and then press the direction key, remember how the vim inside how to switch the various panels? Tmux can also, because it supports mappings. Change the prefix mapping to ctrl+x, and then add the following several statements, now the switch window and vim touch the same, immediately feel a lot of kindness.
#up
Bind-key k Select-pane-u
#down
Bind-key J select-pane-d
#left
Bind-key h select-pane-l
#right
Bind-key L SELECT-PANE-R
The last statement above will change the function of the c-x L, I like this function, because we are very often in two windows or the two panels switch, so I added the following statement
#select last Window
Bind-key c-l Select-window-l
Now my L key can not be casually pressed, ctrl+x L is a switchboard, ctrl+x ctrl+l Switch window, ctrl+l clear screen.
Replication Mode Copy-mode
prefix [Enter copy mode
Start copying by space, move cursor Select Copy Area
Press Enter to copy and exit Copy-mode.
Move the cursor to the specified position, press Preifx] to paste
If we compare Tmux to Vim, then most of the time we are in the editing mode, we can copy the same time as vim to move it? Just add the following line to the configuration file (~/.tmux.conf).
#copy-mode Set the shortcut key to VI mode
Setw-g Mode-keys VI
#会话
C-x s view and select a session as a menu
C-x: New-session Create a new session
C-x D exit and save session
Terminal Run Tmux attach return session
#命名会话
Tmux new-s Session
Tmux new-s session-d #在后台建立会话
Tmux ls #列出会话
Tmux attach-t Session #进入某个会话
#使当前pane maximized
Since I started with vim, I believe there are some people in the world who know what you want more than yourself, so Joe can make the product that screams the world, so I often browse the master's. vimrc file, I used tmux less than 3 days to find out, how can it not maximize the current pane? Just like the Zommwinplugin.vim plugin in vim, you press a key and the current window is maximized to get the maximum visual interface. So far, I have not found that I have the demand and others do not, the same, has been found by the previous solutions, the method is as follows:
# Zoom Pane <-> window
#http://tmux.svn.sourceforge.net/viewvc/tmux/trunk/examples/tmux-zoom.sh
Bind ^z Run "tmux-zoom"
##
#滚屏
Roll screen to enter the Copy-mode, that is, the prefix +[, and then you can use the up and down keys to scroll the screen, the configuration of the VI shortcut key mode, you can like the operation of the VI to scroll the screen, very convenient. Exit directly by pressing ' Q ' key.
#其他配置
Here is the excerpt from someone else's configuration, anyway I do not need to keep the reference bar, perhaps the screen before you need it.
#
# Author:xu Xiaodong <[email protected]>
# modified:2012 Jul 22
#

#--Base Settings--#
Set-g default-terminal "Screen-256color"
SET-G Display-time 3000
Set-g Escape-time 0
Set-g History-limit 65535
Set-g Base-index 1
Set-g Pane-base-index 1

#--Bindkeys--#
# prefix key (CTRL + a)
SET-G prefix ^a
Unbind ^b
Bind a Send-prefix

# Split Window
Unbind ' "'
# vertical split (prefix-)
Bind-splitw-v
Unbind%
Bind | Splitw-h # horizontal split (prefix |)

# Select Pane
Bind K Selectp-u # above (prefix k)
Bind J selectp-d # below (prefix j)
Bind H Selectp-l # Left (prefix h)
Bind L Selectp-r # Right (prefix L)

# Resize Pane
Bind-r ^k resizep-u # upward (prefix ctrl+k)
Bind-r ^j resizep-d # downward (prefix ctrl+j)
Bind-r ^h Resizep-l # to the left (prefix ctrl+h)
Bind-r ^l Resizep-r # to the right (prefix ctrl+l)

# Swap Pane
# Swap with the previous pane (prefix ctrl+u)
Bind ^u Swapp-u
# Swap with the next pane (prefix ctrl+d)
Bind ^d swapp-d

# Misc
# Select the last pane (prefix e)
Bind e LASTP
# Select the last window (prefix ctrl+e)
Bind ^e Last
# Kill pane (prefix q)
Bind Q KILLP
# Kill window (prefix ctrl+q)
Bind ^q KILLW

# Copy Mode
# Enter copy mode (prefix Escape)
Bind Escape Copy-mode
# Paste buffer (prefix ctrl+p)
Bind ^p Pasteb
# Select (v)
Bind-t Vi-copy v begin-selection
# Copy (y)
Bind-t vi-copy y copy-selection

# Zoom Pane <-> window
#http://tmux.svn.sourceforge.net/viewvc/tmux/trunk/examples/tmux-zoom.sh
Bind ^z Run "tmux-zoom"

# app
# htop (prefix!)
Bind! SPLITW Htop
# Man (prefix m)
Bind m command-prompt "SPLITW ' exec man percent '"
# perl func (prefix @)
Bind @ command-prompt "SPLITW ' exec perldoc-t-f-percent '"
# perl var (prefix *)
Bind * command-prompt "SPLITW ' exec perldoc-t-v-percent '"
# perl doc (prefix%)
Bind% command-prompt "SPLITW ' exec perldoc-t percent '"
# Ruby Doc (prefix/)
Bind/command-prompt "SPLITW ' exec ri percent '"

# reload config (prefix r)
Bind r source ~/.tmux.conf \; Display "Configuration reloaded!"

#--StatusBar--#
Set-g Status-utf8 on
Set-g Status-interval 1
Set-g Status-keys VI

Setw-g Mode-keys VI
Setw-g Automatic-rename off

#--ColorScheme--#
#https://github.com/daethorian/conf-tmux/blob/master/colors/zenburn.conf

# modes
Setw-g Clock-mode-colour colour223
Setw-g mode-attr Bold
Setw-g MODE-FG colour223
Setw-g MODE-BG colour235

# panes
Set-g PANE-BORDER-BG colour234
Set-g PANE-BORDER-FG colour234
Set-g PANE-ACTIVE-BORDER-BG colour232
Set-g PANE-ACTIVE-BORDER-FG colour232

# StatusBar
Set-g Status-justify Centre
Set-g STATUS-BG colour235
Set-g STATUS-FG colour248
Set-g Status-attr Dim
#[default]»#[fg=colour187] #S #[default] #[fg=colour187]w#i.p#p#[default] "
Set-g Status-left "
Set-g status-left-attr Bright
Set-g Status-left-length 20
#[fg=colour174]# (/home/xiaodong/bin/uptime) #[default] #[fg=colour174]# (cut-d "-F 1-3/proc/loadavg)"
Set-g Status-right "
Set-g status-right-attr Bright
Set-g Status-right-length 80

Setw-g WINDOW-STATUS-CURRENT-FG colour223
Setw-g WINDOW-STATUS-CURRENT-BG colour237
Setw-g window-status-current-attr Bold
Setw-g Window-status-current-format "#I: #W #f"

#setw-G window-status-alert-attr Bold
#setw-G WINDOW-STATUS-ALERT-FG colour255
#setw-G WINDOW-STATUS-ALERT-BG colour160

# messages
Set-g message-attr Bold
Set-g MESSAGE-FG colour223
Set-g MESSAGE-BG colour235
Set-g visual-activity on
#快速启动tmux
If you think you should open tmux every time, and then open a few windows and panels is very troublesome, then the following script you will love. Refer here
#!/bin/sh
#
# Name:tmuxen, Tmux Environment made easy
# author:xu Xiaodong [email protected]
# LICENSE:GPL
# created:2012 Jul 01
# modified:2012 Jul 02
#

cmd=$ (which tmux) # Tmux Path
Session=codefun # Session Name

If [-Z $cmd]; Then
echo "You need to install Tmux."
Exit 1
Fi

$cmd has-t $session

If [$?! = 0]; Then
$cmd new-d-N vim-s $session "Vim"
$cmd splitw-v-P 20-t $session "pry"
$cmd neww-n mutt-t $session "Mutt"
$cmd neww-n irssi-t $session "Irssi"
$cmd neww-n cmus-t $session "CMUs"
$cmd neww-n zsh-t $session "Zsh"
$cmd splitw-h-P 50-t $session "Zsh"
$cmd selectw-t $session: 5
Fi

$cmd att-t $session

Exit 0
If there are other needs, you can go here to see, of course, the most comprehensive reference document, is Man Tmux. Happy tmux!

How to use Tmux and personalize configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.