Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
How do you activate Vi keybindings in tmux? –  romainl Feb 29 '12 at 6:40
    
add "set-window-option -g mode-keys vi" to your ~/.tmux.conf –  Srećko Toroman Apr 29 '13 at 20:45
1  
It looks like the answers aren't getting to your question of using visual block mode - "multiple lines, but not full width" is also what I'm looking for. Did you find your own solution? –  dmonopoly Apr 23 at 14:38

4 Answers 4

You can select a visual block just like in vi -- type v before starting the selection. In the manpage, this is called "Rectangle toggle".

share|improve this answer
2  
That exactly what I'm using. But that has a problem which I described above. The rectangle is always full width. I need a VIM-like rectangle (x1, y1) - (x2, y2) :) –  Srećko Toroman Apr 29 '13 at 20:44
    
@Srećko Toroman what do you mean exactly with full width? –  rednaw Jan 2 at 16:24

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:

  • 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.
share|improve this answer

As Lars points out, v actually does what you are asking for by toggling between rectangle mode and full-line selection mode. space and enter are used respectively to start a new selection and copy it. Enter copy mode using your prefix followed by [ and paste your copied selection into any tmux window using ]

This confused me also since v functions in vim as space does in tmux , to begin the selection.

Issuing :list-keys -t vi-copy in tmux will show the full table of commands in copy mode.

bind-key -t vi-copy    C-b page-up                                                      
bind-key -t vi-copy    C-c cancel
bind-key -t vi-copy    C-e scroll-down
bind-key -t vi-copy    C-f page-down
bind-key -t vi-copy    C-h cursor-left
bind-key -t vi-copy  Enter copy-selection
bind-key -t vi-copy    C-y scroll-up
bind-key -t vi-copy Escape clear-selection
bind-key -t vi-copy  Space begin-selection
bind-key -t vi-copy      $ end-of-line
bind-key -t vi-copy      , jump-reverse
bind-key -t vi-copy      / search-forward
bind-key -t vi-copy      0 start-of-line
bind-key -t vi-copy      1 start-number-prefix
bind-key -t vi-copy      2 start-number-prefix
bind-key -t vi-copy      3 start-number-prefix
bind-key -t vi-copy      4 start-number-prefix
bind-key -t vi-copy      5 start-number-prefix
bind-key -t vi-copy      6 start-number-prefix
bind-key -t vi-copy      7 start-number-prefix
bind-key -t vi-copy      8 start-number-prefix
bind-key -t vi-copy      9 start-number-prefix
bind-key -t vi-copy      : goto-line
bind-key -t vi-copy      ; jump-again
bind-key -t vi-copy      ? search-backward
bind-key -t vi-copy      B previous-space
bind-key -t vi-copy      D copy-end-of-line
bind-key -t vi-copy      E next-space-end
bind-key -t vi-copy      F jump-backward
bind-key -t vi-copy      G history-bottom
bind-key -t vi-copy      H top-line
bind-key -t vi-copy      J scroll-down
bind-key -t vi-copy      K scroll-up
bind-key -t vi-copy      L bottom-line
bind-key -t vi-copy      M middle-line
bind-key -t vi-copy      N search-reverse
bind-key -t vi-copy      T jump-to-backward
bind-key -t vi-copy      W next-space
bind-key -t vi-copy      ^ back-to-indentation
bind-key -t vi-copy      b previous-word
bind-key -t vi-copy      e next-word-end
bind-key -t vi-copy      f jump-forward
bind-key -t vi-copy      g history-top
bind-key -t vi-copy      h cursor-left
bind-key -t vi-copy      j cursor-down
bind-key -t vi-copy      k cursor-up
bind-key -t vi-copy      l cursor-right
bind-key -t vi-copy      n search-again
bind-key -t vi-copy      q cancel
bind-key -t vi-copy      t jump-to-forward
bind-key -t vi-copy      v rectangle-toggle
bind-key -t vi-copy      w next-word
bind-key -t vi-copy BSpace cursor-left
bind-key -t vi-copy  NPage page-down
bind-key -t vi-copy  PPage page-up
bind-key -t vi-copy     Up cursor-up
bind-key -t vi-copy   Down cursor-down
bind-key -t vi-copy   Left cursor-left
bind-key -t vi-copy  Right cursor-right
bind-key -t vi-copy   C-Up scroll-up
bind-key -t vi-copy C-Down scroll-down
share|improve this answer
    
I don't get it again. Given a Vim window with a vertical split, how do you select a rectangle in the first split only? Here, the multiline rectangle will always wrap the full Tmux pane. It's not like a Vim block selection. I'm using tmux 1.5, maybe it's different in your version? –  Srećko Toroman Jul 3 '13 at 16:36
    
Yeah, here, v is mapped to begin-selection. The rectangle-toggle is undefined. So I guess both of you are right, I just need to update my client? –  Srećko Toroman Jul 3 '13 at 16:39
    
If you have a split vim window open in a single tmux pane, can't you use vim's yank and paste ability? If on the other hand you have two tmux panes, each with it's own vim window, tmux select/copy should be able to wrap at the pane edges. I'm using tmux 1.7 –  here Jan 28 at 6:13

EDIT: After checking the source code, I've re-written my answer completely.

I originally thought this wasn't possible because I couldn't get this to work, but the other answers are correct about v being the correct key for this, but it's not obvious how to use this, especially if you are used to vim.

Also, it wouldn't work for me because I had v bound to begin-selection - I think I must have blindly copied this idea from somewhere else to mimic the vim way to start a character wise selection, not knowing it would cause problems further down the road with block selections.

$ tmux -V
tmux 1.9a
$ tmux list-keys -t vi-copy  | grep " v "
bind-key -t vi-copy      v begin-selection     # suppressed my block selection mode

Once I took away this binding (by commenting out the line in my .tmux.conf and restarting tmux), it started to work, but it's still rather confusing because it's not the same as vim at all.

With default bindings, it should work as follows:

  1. Make sure you have tmux unning with setw -g mode-keys vi in your .tmux.conf.
  2. type C-a [ to start copy mode
  3. move the cursor to where you want to begin the selection
  4. type v
  5. type Space to start the selection
  6. move the cursor and you will see a rectangular selection
  7. you can press v again to toggle the selection back into character-wise selection or vice-versa.

You don't have to type the v before you press Space. You could start the the character-wise selection with Space first and press v afterwards to toggle to rectangular selection mode.

It's quite different from vim in that in tmux you have to type two characters: Space and v to get into rectangular selection mode. In vim you only have to type Ctrl-V.

Also in vim v starts a character-wise selection, not block.

Sadly, there doesn't seem to be a line-wise selection mode in tmux (Shift-v in vim).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.