Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Linux \ Tmux Crash Course: Tips and adjustment     - Ubuntu buffalo wzr-hp-300nh brush DD-WRT router system (Linux)

- Five kinds of IO Model under Linux (Programming)

- Create, modify, delete users, user groups under linux (Linux)

- hexdump Linux command (Linux)

- Ubuntu 15.04 installed Nvidia Quadro series graphics driver (Linux)

- Use calcurse schedule appointments and to-do in the Linux terminal (Linux)

- Nodejs command-line program development tutorial (Programming)

- Solaris 10 installation configuration mrtg monitoring system (Linux)

- Android Launcher3 Application List Modify a transparent background (Linux)

- How do you temporarily emptied Bash environment variable before running the command on Linux (Linux)

- DataGuard add temporary data files bug (Database)

- The practical application of Oracle synonyms + dblink (Database)

- Java programmers talk about those advanced knowledge and direction (Programming)

- Linux environment variable configuration and save places (Linux)

- Linux System Getting Started Learning: modify environment variables in Linux PATH (Linux)

- History and Statistics tuptime use tools to view Linux server system boot time (Server)

- Install Web-based monitoring tool: Linux-Dash (Server)

- Bash How to read a file line by line (Programming)

- Java reflection summary (Programming)

- Linux GCC 5.1.0 compiler installation (Linux)

  Tmux Crash Course: Tips and adjustment
  Add Date : 2018-11-21      
  Brief introduction

Some developers often want to use a terminal console work, leading to the final open too many tabs. If you are one of them, or you are practicing pair programming, then I recommend you read this article. From last month, I began extensive use Tmux Tmux and found very useful, so I think we should write an article to share some advice and professional programs related to the use Tmux with you. This article will introduce what Tmux yes, then explain how to use Tmux, for it to be combined with Vim, to create a more efficient, more elegant terminal tool.

This article will contain the following:

The foundation Tmux
Tmux the best features
Window (Window)
Pane (Pane)
Session (Session)
Quickly move the cursor between text or copy text
Very lightweight pair programming
Adjustment Tmux to enhance its integration with the Vim
Adjust the background color scheme
Adjust the shape of the cursor
Adjust the text pasted indent
Other tools can enhance the experience or skills Tmux
Automatically creates a session with Tmuxinator
Change the color of the status bar Tmux
Note that in the process of writing this article, I installed the following software in this group, and, when tested using these versions:

Tmux 1.9a
Vim 7.4
iTerm 2.1
Mac OS (Mavericks and Yosemite)
let's start!


What is Tmux?

Tmux is a tool used to run in a terminal window plurality of terminal session. Not only that, you can also make Tmux terminal session running in the background or on-demand access, the session is disconnected, this feature is very useful. Later, we will see how to take full advantage of this feature.

This is a conversation Tmux

On the left: Vim
Right: System Shell
Bottom left: Tmux session name ( "pomodoro-app")
Central below: The current session Tmux window ( "app log", "editor" and "shell")
Bottom right: the current date
How to install Tmux?

On Mac OS installation:

Install Homebrew

ruby -e "$ (curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
For more information about installing homebrew can be found here.

Installation Tmux

$ Brew install tmux

In Ubuntu installation:

In the terminal enter the following command:

$ Sudo apt-get install tmux

Tmux shortcut prefix (Prefix)

In order to make their own shortcuts and shortcuts without disturbing each other software, Tmux provides a shortcut prefix. When you want to use shortcut keys, you need to press the shortcut key prefix, and then press the shortcut key. Shortcuts Tmux prefix used by default is the key combination Ctrl-b (press Ctrl and b keys). For example, if you want a list of keyboard shortcuts Tmux in the current session (the corresponding shortcut key is s), then you only need to do the following steps:

Press the key combination Ctrl-b (Tmux shortcut prefix)
Release the key combination Ctrl-b
Press the s key
Here are some tips:

First, I recommend swap Ctrl and Caps-Lock (Caps Lock) key.

By pressing the Caps-Lock key instead of the Ctrl key will be very useful. Because the encoding process, you need frequent pressing the Ctrl key, and because the Caps-Lock finger at the start of the keyboard in the same straight line, so press the Caps-Lock key is easier and convenient.

Secondly, I propose Tmux shortcut prefix becomes Ctrl - a. Replace the Ctrl key with the Caps-Lock key after due Caps-Lock key and a key closer together, so press Ctrl - a ratio it will press Ctrl - b easier and more convenient.

To change the prefix shortcut Ctrl-a, add the following configuration is added to Tmux profile ~ / .tmux.conf in:

unbind C-b
set-g prefix C-a

Tmux profile

Whenever open a new session, Tmux will first read ~ / .tmux.conf this document. The file is stored on Tmux configuration.

Tip: If you want the new configuration can take effect immediately, you can configure the following line added to the file ~ / .tmux.conf in.

# Bind a reload key
bind R source-file ~ / .tmux.conf \; display-message "Config reloaded .."
After such a configuration, each time a ~ / .tmux.conf file to add a new configuration, just press Ctrl-b r can reload the configuration and the new configuration to take effect, eliminating the need to open a new session.

Tmux the best features

Tip: This screenshot below and you might see when using Tmux interface is slightly different. This is because I changed Tmux status bar configuration, if you also want to revise and screenshots of the same effect, you can refer to "beautify Tmux status bar" in this section of the steps.


I think vertically split screen is a good idea, so I can use Vim on one side, while on the other side to see the code operating results, if need be, and sometimes I will then open a console. Here I will explain how to use Tmux achieve all this.

On the left: Vim (the upper left is a Ruby class file, written in the lower left is for this type of test file)
Right: a Bash session
After creating a vertically disposed panes easily, to be opened a Tmux session, simply press Ctrl-b%, a vertical pane appears. In addition, if want to split the screen horizontally, you only need to press Ctrl-b ". In the pane to move the cursor between Tmux is also very simple, just press the first Tmux shortcut prefix, and then press the corresponding it can make the cursor arrow keys to enter the target pane.


In Tmux, the window is a pane container, you can place multiple panes in the window, according to your actual needs and arrange multiple panes in the window is completely depends on your needs. For example, I often do, to turn on the server called a "server" window used to run the application (in this window you can see the log server), then open another called "editor" window with in writing code. There are two panes in this window, one for Vim, used to run a test code. Finally, open a place called "shell" of the window is used by Bash shell to run the command. Tmux window feature is very useful, because in a window, you can create multiple panes in a window so you can see all of the content pane, you can effectively use the limited screen space by this method.

In Tmux session, the existing window will be listed at the bottom of the screen. Shown below it is in default mode Tmux lists existing window. Here a total of three windows, namely the "server", "editor" and "shell".

To create a window, just press Ctrl-b c; To switch windows, simply first press Ctrl-b, then press want to switch to the window corresponding to the number that will be next to the window name display.


Tmux a session can contain multiple windows. Session feature is very simple to use, for example, you can create a dedicated Tmux session for a specific project. To create a new session, just run the following command in the terminal:

$ Tmux new-s
Suppose I need to develop another project, so I'll do this again to create a new session. Although access to the new session, but the original session did not go away. So I could go back to work until later in the session. To create a new session, just press Ctrl-b:, then enter the following command:

Unless explicitly close the session, otherwise Tmux session before rebooting the computer will not disappear. As long as there is no restart your computer, you are free to jump from one project to another session.

Switch between sessions Tmux

To get a list of existing session, you can press Ctrl-b s. Shown below is a list of the session:

Each list has a session ID, which is starting from zero. Press the corresponding ID to get into the conversation. If you have created one or more sessions, but has not yet run Tmux, you can enter the following command to access the conversation turned on.

$ Tmux attach

Quickly move the cursor between text, copy text

In iTerm2 in order to quickly copy the contents will have to use the keyboard and mouse together, which I have been very fond of. I think there will be no need to use the mouse and more efficient replication methods. Fortunately, Tmux provides the only complete copy of the keyboard can function, which stems Tmux be started from command line, and the command line interface can not use a mouse.

Move the cursor between text

In Tmux Vim can be used very similar way to move the cursor across the text. As you know well, as with the k key to move the cursor to the previous line, you can move backward a word with the w key and so on. But also by the Tmux to vi mode so that it is closer to the operation of Vim. To this end, the following configuration needs to be added to the file ~ / .tmux.conf in.

# Use vim keybindings in copy mode
setw -g mode-keys vi

Copy down the text sent to the system clipboard

By default, when copying text from Tmux, copy down the text can be pasted into the same Tmux session. To make the copy down the text can be pasted into any position, you need to copy the text to the clipboard Tmux system. To this end, we need to do:

Installation retach-to-user-namespace. With brew install it will be very simple, just run the following command:

$ Brew install reattach-to-user-namespace
Add the following in the configuration file ~ / .tmux.conf in:

# Invoke reattach-to-user-namespace every time a new window / pane opens
set-option -g default-command "reattach-to-user-namespace -l bash"

Select and copy text

Now that you have set to vi mode, also installed rettach-to-user-namespace, let's look at how to copy text from Tmux sessions it. Suppose you want to copy the IP address, so let's run the ifconfig command. Next, please follow the following steps:

First press Ctrl-b [enter Copy mode, and then you can see a small highlighted text appears in the upper right corner of the screen ( "[0/0]")

Then you can use the same image in Vim j, k, l, and so h keys to move the cursor across the text.

Move the cursor to the text you want to copy and then press the space key to start selecting text (this step and copy the text in Vim exactly the same).

Select the text you want to copy and then finished press Enter.

Such IP address is copied down and can be pasted into any place.

Copy the text to make operations more like Vim

You can also set Tmux v key to select text, copy text using the y key. For this reason only you need to configure the following entry to the configuration file ~ / .tmux.conf in.

# Start selecting text typing 'v' key (once you are in copy mode)
bind-key -t vi-copy v begin-selection
# Copy selected text to the system's clipboard
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"
Efficient pair programming

You can share Tmux session address to others so they can access the session through the SSH. Because the session is based on SSH, so no additional overhead. By using high-speed Internet connection to a remote session for those users who, they will find this session it is to run locally.

Use Tmate in Tmux

Tmate Tmux is a management tool that not only can easily create Tmux session but also to the session via the Internet to share with others. To use Tmate shared Tmux session, follow these steps:

Install Homebrew

$ Ruby -e "$ (curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Installation Tmate

$ Brew update && \
brew tap nviennot / tmate && \
brew install tmate
Use Tmate open a new session

$ Tmate
Copy SSH URL generated by the Tmate from Tmux session. As shown in the following figure, note the bottom of the screen information "[tmate] Remote session: ssh ...":

Use just copy down the URL you can invite others to access your session through the SSH.

Learn how to use Tmux pair programming function after, you can then use your favorite voice services provided by the operator to further strengthen interaction session.

Adjustment Tmux to enhance its integration with the Vim

Adjust the background color scheme

When I first opened by Tmux Vim, Vim I found the color is not displayed correctly. As the chart below, there is only a place to have character background color.

This problem is due to the need to configure a special terminal parameters (term parameter) by Tmux running Vim. Please add the following line to configure your ~ / .vim file.

if exists ( '$ TMUX')
set term = screen-256color
In the updated configuration file ~ / .vimrc later, the color should be able to display correctly.

Adjust the shape of the cursor

By default, when running through Tmux Vim, regardless of the current Vim is in Insert mode, video mode or other mode, the cursor shape is the same. This makes it difficult to judge what the current Vim mode Yes. To avoid this problem, you need to inform iTerm Tmux updates the shape of the cursor. To this end, the following configuration needs to be added to the file ~ / .vimrc in.

if exists ( '$ ITERM_PROFILE')
if exists ( '$ TMUX')
let & t_SI = "\ [3 q"
let & t_EI = "\ [0 q"
let & t_SI = "\ ] 50; CursorShape = 1 \ x7"
let & t_EI = "\ ] 50; CursorShape = 0 \ x7"
Here I would like to thank Andy Fowler, he originally shared adjust the shape of the cursor this technique.

Adjust the text pasted indent

You may encounter when pasting text in Vim such problems, sometimes indented the text will change, especially when you paste a lot of text, this problem will become more apparent. Although you can perform before pasting: set nopaste to solve this problem, but there's a better solution. Below this configuration it is to be added to the configuration file ~ / .vimrc, so that Vim will automatically stop automatically when pasting text indent.

"For tmux to automatically set paste and nopaste mode at the time pasting (as
"Happens in VIM UI)
function! WrapForTmux (s)
if! exists ( '$ TMUX')
return a: s
let tmux_start = "\ < Esc > Ptmux;"
let tmux_end = "\ < Es c> \\"
return tmux_start substitute. (a: s, "\ < Esc >", "\ < Esc > \ < Esc >", 'g') tmux_end.
let & t_SI. = WrapForTmux ( "\ < Esc > [? 2004h")
let & t_EI. = WrapForTmux ( "\ < Esc > [? 2004l")
function! XTermPasteBegin ()
set pastetoggle = < Esc > [201 ~
set paste
return ""
inoremap < special > < expr > < Esc > [200 ~ XTermPasteBegin ()
Here I would like to thank Marcin Kulik, he originally shared this technique.

Other tools can enhance the experience or skills Tmux

Tmuxinator (automatically create a session for the project)

Suppose you are developing an application A. In the development process, often to create Tmux session, which contains the "server", "editor" (used for writing code) and "shell" (used to run system commands) these three windows. Moreover, at a particular time of day you need temporary access to the development of the application B's. So you had to create another session, although there are slight differences (for example, directories and some commands), but the session is to be included in the application A that three windows. But with Tmuxinator, you can declare a session for each Tmux configuration, and then use a command will be able to create out of this session of the. This feature is great, is not it.

Tmuxinator the gem is a Ruby package for creating Tmux session. The way it works is to define the details of the session in the configuration file, and then create these sessions with one command. Let's look at how to install and how to add Tmuxinator configured to open a session for the specified item. You can run the following command to install the gem Tmuxinator package.

$ Gem install tmuxinator
Installed after the Tmuxinator, you can run tmuxinator or mux in the Shell command in the system. Let the above applications (there are three windows, namely the "servers", "editor" and "shell") to create a profile bars. The following command role is to create and open a configuration file for the project.

$ Tmuxinator new project_a
After pressing Enter, it will automatically open the file ~ / .tmuxinator / project_a.yml. To achieve the desired configuration item A, you need to update the contents of project_a.yml:

name: project_a
root: < the-folder-of-project-A >
- Server: < command-to-start-application-server >
- Editor:
layout: even-horizontal
- Vim
- < Command-to-launch-tests-guard >
- Shell: ''
Once added to the above configuration item A Yaml file, simply run the following command can start a conversation Tmux.

$ Tmuxinator start project_a
Of course, if you wish, you can also use the alias Tmuxinator command:

$ Tmuxinator start project_a
Done. Now, whenever you want to enter the A coding project when you only need to run Tmuxinator command.

Tmuxinator can go here to see the official documents.

Beautify Tmux status bar

By default, Tmux status bar looks like this is the following figure:

We needed to change the appearance of the status bar. For me, I like the figure below this refreshing look.

In order to achieve the desired effect on the map, I will be the following line to be added to the configuration file ~ / .tmux.conf in.

# Status bar
# Colors
set-g status-bg black
set-g status-fg white
# Alignment
set-option -g status-justify centre
# Spot at left
set-option -g status-left '# [bg = black, fg = green] [# [fg = cyan] # S # [fg = green]]'
set-option -g status-left-length 20
# Window list
setw -g automatic-rename on
set-window-option -g window-status-format '# [dim] #I: # [default] # W # [fg = grey, dim]'
set-window-option -g window-status-current-format '# [fg = cyan, bold] # I # [fg = blue]: # [fg = cyan] # W # [fg = dim]'
# Spot at right
set-g status-right '# [fg = green] [# [fg = cyan]% Y-% m-% d # [fg = green]]'
to sum up

In this article we first introduced the basic functions Tmux then introduced Tmux the best in several functions. After that describes some of the configurations and several tools can enhance Tmux experience. At this point, gentlemen, for Tmux impression of how it? Do you also find something else useful features or configurations? If so welcome message to tell us.
- RedHat Linux source code package installation process (Linux)
- CentOS 6.6 command-line automatic completion (Linux)
- Shell script to delete empty folders recursively (Linux)
- Echo Command Examples (Linux)
- Linux and SELinux Exploration Program Manager (Linux)
- Python several standard types of built-in functions (Programming)
- ethtool implementation framework and application in Linux (Linux)
- Oracle set the timer task JOB scheduler to execute stored procedures or PL / SQL code block (Database)
- Install Firefox 28 on Ubuntu, Linux Mint (Linux)
- CentOS 5.8 (64) Python 2.7.5 installation error resolved (Linux)
- How to Install Redis server on CentOS 7 (Server)
- Using Oracle for Oracle GoldenGate to achieve a one-way data synchronization (Database)
- Syncthing: Private Security Synchronization tool to synchronize files between computers / folder (Linux)
- Linux server is how to do after the invasion (Linux)
- To install Gitolite in Ubuntu / Fedora / CentOS (Linux)
- System Safety: Windows and Linux platforms (Linux)
- Node.js development environment deployment (Server)
- Detailed usage history command (Linux)
- Compare Several MySQL environmental issues (Database)
- Ubuntu install ruby (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.