tmux’s Vi mode lets you navigate its panes, windows, and sessions with the same familiar keybindings as Vim.
# ~/.tmux.conf
setw -g mode-keys vi
setw -g status-keys vi
This configures both the general mode keys and the status line keys to use Vi bindings.
Let’s say you’ve got a few windows open in tmux. You can switch between them using Ctrl-a n for next and Ctrl-a p for previous. But with Vi mode, you can enter copy mode (Ctrl-a [), navigate to the window list at the bottom of your status bar, and then use j and k to move up and down the list of windows. Pressing Enter selects the highlighted window. It feels more integrated, like you’re just extending your Vim workflow.
Here’s how it looks in action. Imagine you’re in a tmux session with three windows: one running htop, another with a vim session, and a third with a bash shell.
# Window 0: htop
# Window 1: vim
# Window 2: bash
Normally, you’d cycle through with Ctrl-a n or Ctrl-a p. But in Vi mode, you can:
- Press
Ctrl-a [to enter copy mode. - Type
/htopand pressEnterto search for the window containinghtop. This will highlight it in the status bar. - Press
Enteragain to switch to thehtopwindow.
This search functionality, directly from Vim’s command line, is a game-changer for navigating complex tmux layouts.
The core idea is that tmux’s modes, particularly copy mode and command mode (entered via Ctrl-a :), can be configured to use Vi’s keybindings. This means commands like h, j, k, l for movement, w and b for word movement, / for search, and ? for reverse search become available not just within Vim, but within tmux itself for interacting with tmux’s interface.
When you’re in copy mode (Ctrl-a [), you can move your cursor around the screen as you would in Vim. This is useful for selecting text to copy. Press v to start a visual selection (like in Vim), use hjkl to move, and then press y to yank the selected text into tmux’s buffer. You can then paste it elsewhere in tmux using Ctrl-a ].
The status bar (setw -g status-keys vi) also adopts Vi bindings. This means when you press Ctrl-a : to enter tmux’s command mode, you can use hjkl to move your cursor along the command line, Ctrl-f and Ctrl-b to page through history, and / to search command history.
Consider the mode-keys vi setting. This applies to all tmux modes that involve text input or navigation, primarily copy mode. When mode-keys vi is set, tmux interprets h, j, k, l as directional movements, w to move forward a word, b to move backward, G to go to the end of the buffer, and gg to go to the beginning. This is distinct from status-keys vi, which specifically affects the status line when you’re entering commands or viewing status information.
The true power emerges when you combine these. You can be in a pane, enter copy mode (Ctrl-a [), search for a specific string within the pane’s scrollback buffer using /your_search_term, navigate to that line, and then press Enter to set the cursor position. From there, you can start a visual selection (v) and yank a block of text. This same text can then be pasted into another pane (Ctrl-a ]) or even into a Vim buffer if Vim is configured to interact with tmux’s paste buffer.
Many users are unaware that tmux’s visual selection in copy mode (v) can be extended with Vim’s motion commands. For instance, after pressing v, you can type f<char> to move to the next occurrence of <char> on the current line, or t<char> to move to the character before the next occurrence. This allows for precise selection of text that isn’t just line-based.
The next logical step is to explore tmux’s scripting capabilities and how Vi mode can be integrated into custom keybindings for even more fluid control.