Tmux copy mode – select text block

tmuxvim

I'm using tmux with vi keys, and copy/paste works fine as long as you're selecting one line of text, or if you have only one window. When I want to select a block of text (multiple lines, but not full width), then I run into trouble. If I have multiple vim windows opened, then it would select text from all of them, and what I need is to select text only from one window. That is, just like a visual select in vim.

Is this possible to do?

Best Answer

Make sure to check the bottom of this post for necessary bindings that need to go into your .tmux.conf file.

I am assuming your prefix key is C-a:

  • C-a means: press Ctrl + A
  • C-a [ means: press Ctrl + A then press [

To do a rectangle selection of text from (1,1) to (2,2) in tmux:

  • Go to the copy mode: C-a [
  • Move the middle of a line
  • Press C-v
  • Press Space
  • Move the selection with jkhl
  • Once you are happy with your selection press Enter (or y if you have the binding in your conf file).
  • You can paste the latest copy buffer by: C-a ]

Notice that pressing space is necessary for rectangle selection.

To select lines like you would normally do, go the copy mode, and press v, select with jkhl keys and press y.

I have these bindings in my .tmux.conf:

Prior to version 2.4 (20 April 2017):

setw -g mode-keys vi
bind-key -t vi-copy 'v' begin-selection     # Begin selection in copy mode.
bind-key -t vi-copy 'C-v' rectangle-toggle  # Begin selection in copy mode.
bind-key -t vi-copy 'y' copy-selection      # Yank selection in copy mode.

After version 2.4:

setw -g mode-keys vi
bind-key -T copy-mode-vi 'v' send -X begin-selection     # Begin selection in copy mode.
bind-key -T copy-mode-vi 'C-v' send -X rectangle-toggle  # Begin selection in copy mode.
bind-key -T copy-mode-vi 'y' send -X copy-selection      # Yank selection in copy mode.

It is important to unbind default rectangle-toggle binding:

unbind-key -t vi-copy v  # Prior to version 2.4
unbind-key -T copy-mode-vi v

Otherwise new 'C-v' binding doesn't work.

Note: to have a single .tmux.conf which works across versions, see this question.

Related Question