配置(Configuration ){#config}

Most tmux users break away from the defaults by creating their own customized configurations. These configurations vary from the trivial, such as adding keybindings, and adjusting the prefix key, to complex things, such as decking out the status bar with system stats and fancy glyphs via powerlines.

Configuration of tmux is managed through .tmux.conf in your $HOME directory. The paths ~/.tmux.conf and $HOME/.tmux.conf should work on OS X, Linux, and BSD.

Configuration is applied upon initially starting tmux. The contents of the configuration are tmux commands. The file can be reloaded later via source-file, which is discussed in this chapter.

For a sample config, I maintain a pretty decked out one at https://github.com/tony/tmux-config. It’s permissively licensed, and you’re free to copy and paste from it as you wish.

Custom Configs

You can specify your config via the -f command. Like this:

    $ tmux -f path/to/config.conf

Note: If a tmux server is running in the background and you want to test a fresh config, you must either shut down the rest of the tmux sessions or use a different socket name. Like this:

 $ tmux -f path/to/config.conf -Ltesting_tmux

And you can treat everything like normal; just keep passing -Ltesting_tmux (or whatever socket name you feel like testing configs with) for reuse.

 $ tmux -Ltesting_tmux attach

重载配置文件 {#reload-config}

You can apply config files in live tmux sessions. Compare this to source or “dot” in the POSIX standard.

Prefix + : will open the tmux prompt, then type:

:source /path/to/config.conf

And hit return.

$ tmux source-file /path/to/config.conf can also achieve the same result via command line.

简便加载

Even better, often, you will keep your default tmux config stored in $HOME/.tmux.conf. So, what can you do? You can bind-key to source-file ~/.tmux.conf:

bind r source ~/.tmux.conf

You can also have it give you a confirmation afterwards:

bind r source ~/.tmux.conf\; display "~/.tmux.conf sourced!"

Now, you can type Prefix + r to get the config to reload.

Note that reloading the configuration only re-runs the configuration file. It will not reset keybindings or styling you apply to tmux.

配置文件的工作原理

The tmux configuration is processed just like run commands in a ~/.zshrc or ~/.bashrc file. bind r source ~/.tmux.conf in the tmux configuration is the same as $ tmux bind r source ~/.tmux.conf.

You could always create a shell script prefixing tmux in front of commands and run it on fresh servers. The result is the same. Same goes if you manually type in $ tmux set-option and $ tmux bind-key commands into any terminal (in or outside tmux).

This in .tmux.conf:

    bind-key a send-prefix

Is the same as having no .tmux.conf (or $ tmux -f/dev/null) and typing:

    $ tmux bind-key a send-prefix

in a newly started tmux server.

The important thing to internalize is that a tmux configuration consists of setting server options (set-option -s), global session (set-option -g), and window options (set-window-option -g).

The rest of this chapter is going to proceed cookbook-style. You can pick out these tweaks and add them to your .tmux.conf and reload.

服务设置(Server options)

Server options are set with set-option -s option value.

Tweak timing between key sequences

    set -s escape-time 0

Terminal coloring

If you’re having an issue with color detail in tmux, it may help to set default-terminal to screen-256color.

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

This sets the TERM variable in new panes.

会话设置(Session options)

Aside from the status bar, covered in the next chapter, most user configuration will be custom keybindings. This section covers the few generic options, and the next section goes into snippets involving keybindings.

窗口计数(Base index)

This was mentioned earlier in the book, but it’s a favorite tweak of many tmux users, who find it more intuitive to start their window counting at 1, rather than the default, 0. To set the starting number (base index) for windows:

    set -g base-index 1

Setting base-index assures newly created windows start at 1 and count upwards.

窗口设置(Window options)

Window options are set via set-option -w or set-window-option. They are the same thing.

Automatic window naming

Setting automatic-rename alters the name of the window based upon its active pane:

    set-window-option -g automatic-rename

Automatic renaming will be disabled for the window if you rename it manually.

快捷键绑定(Keybindings)

Prefix key

The default prefix key in tmux is <Ctrl-b>. You can customize it by setting a new prefix and unsetting the default. To set the prefix to <Ctrl-a>, like GNU Screen, try this:

    set-option -g prefix C-a
    unbind-key C-b
    bind-key a send-prefix

New window with prompt

Prompt for window name upon creating a new window, Prefix + C (capital C):

    bind-key C command-prompt -p "Name of new window: " "new-window -n '%%'"

Vi copy-paste keys

This is comprised of two-parts: Setting the mode-keys window option to vi and setting the vi-copy bindings to use v to begin selection and y to yank.

    # Vi copypaste mode
    set-window-option -g mode-keys vi
    bind-key -t vi-copy 'v' begin-selection
    bind-key -t vi-copy 'y' copy-selection

hjkl / vi-like pane traversal

Another one for vi fans, this keeps your right hand on the home row when moving directionally across panes in a window.

    bind h select-pane -L
    bind j select-pane -D
    bind k select-pane -U
    bind l select-pane -R

Further inspiration

For more ideas, I have a .tmux.conf you can copy-paste from on the internet at https://github.com/tony/tmux-config/blob/master/.tmux.conf.

In the next chapter, we will go into configuring the status line.