How to enable vim mode in tmux?

Quick Answer

Enable Vim mode in tmux by adding set-window-option -g mode-keys vi to your ~/.tmux.conf file. This allows you to use vi-style keybindings in copy mode for navigation and text selection.

set-window-option -g mode-keys vi

Detailed Explanation

tmux's copy mode supports two sets of keybindings: Emacs-style (default) and Vi-style. By enabling Vi mode, you can use familiar Vim navigation and selection commands in tmux, which is especially useful for Vim users.

Enabling Vi mode:

Add the following line to your ~/.tmux.conf file:

# Enable Vi mode
set-window-option -g mode-keys vi

# Or the shorter version
setw -g mode-keys vi

Common Vi mode keybindings in copy mode:

After enabling Vi mode and entering copy mode (Ctrl+b [), you can use these key bindings:

h, j, k, lMove cursor left, down, up, right
vStart selection in character mode
VStart selection in line mode
Ctrl+vStart selection in block mode
yYank (copy) selected text
w, b, eMove by word (next, back, end)
0, $Move to start, end of line
/, ?Search forward/backward
n, NNext/previous search match
g, GGo to top/bottom

Customizing Vi mode copy behavior:

Enhance Vi mode by adding these keybindings to your ~/.tmux.conf:

# Setup 'v' to begin selection, just like Vim
bind-key -T copy-mode-vi v send-keys -X begin-selection

# Setup 'y' to yank (copy), just like Vim
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel

# Update the default binding of 'Enter' to use copy-selection-and-cancel
unbind-key -T copy-mode-vi Enter
bind-key -T copy-mode-vi Enter send-keys -X copy-selection-and-cancel

Integrating with system clipboard:

Make Vi mode even more powerful by integrating with your system clipboard:

# For macOS
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "pbcopy"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "pbcopy"

# For Linux with xclip
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"

Pro Tip

If you use Neovim or Vim inside tmux, add this to your ~/.tmux.conf to reduce escape key delay:

# Reduce delay when pressing Escape in Vim
set -sg escape-time 10

Note on Compatibility

When using Vi mode in tmux, you may need to explicitly set Vim's "term" variable for proper color support. Add this to your ~/.vimrc or ~/.config/nvim/init.vim:

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