Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tmux copy mode: how to create your own command?

Tags:

tmux

I love Tmux and its copy mode with Vi commands, but I'm really annoyed by the fact that this mode is very far from being as efficient as real Vim.

For example, there is no keybinding to just copy a word (yw), I must always "go to the beginning of a word" "begin selection", "go to the end of the word" then "finish selection". A lot of operations when I just need to do yw in vim.

I searched a way to create my own "yw" command in Tmux copy mode. Chaining all the operations needed is a good idea, but a simple bind with commands separated by ; just doesn't work (similar thing works in non-copy mode). Is there something I miss? Or is the copy mode of Tmux just limited and not as scriptable as I need it to be?

like image 223
Jooj Avatar asked Jun 08 '12 14:06

Jooj


People also ask

How do you use copy mode in tmux?

To enter the copy mode, use Ctrl + [ . You should be able to see a yellow visual indicator on the top right of your tmux window. With this, you can now move around using vim navigation keys.

How do you enter copy mode?

Enter 'copy mode' by pressing CTRL + b, [. Use the arrow keys to go to the position from where you want to start copying. Press CTRL +SPACE to start copying. Use arrow keys to go to the end of text you want to copy.

How do you get out of copy mode in tmux?

The solution is to use tmux specific controls to access its own scrollback buffer: Ctrl-b then [ to enter copy mode, use Down/Up arrows or PageDown and PageUp keys, q or Enter to exit copy mode.

How do I enable vi mode in tmux?

Default keybinding to get into copy mode is prefix+[ . If you are a vim user you will likely want to use vi style keys, add this to your ~/. tmux. conf file to enable vi mode.


2 Answers

I have this in my tmux conf:

# vi-style controls in copy mode
set-option -g status-keys vi
set-window-option -g mode-keys vi

# v and y like vi in copy-mode
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection

Now after going copy-mode i can easily select words by:

vw

And copy with

y

In tmux you have to select something to copy. There is nothing like copying in normal mode as you know from usual vi/vim commands. Unfortunately you can only use one key (like v or y) for every tmux argument.

You can find more about tmux's vi movement commands here: https://superuser.com/a/197272/57890

like image 104
Fatih Arslan Avatar answered Oct 26 '22 19:10

Fatih Arslan


On upstream (2.4+) tmux version this got changed, in order to create a bindings for begin selection you need to use -T and send-keys with -X.

More info in tmux changelog.

Here my bindings for vi copy mode as an example:

# Bind `v` to trigger selection    
bind-key -T copy-mode-vi v send-keys -X begin-selection

# Bind `y` to yank current selection
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel

# Rebind `mouse click + drag button release` to not jump away from context
bind-key -T copy-mode-vi MouseDragEnd1Pane send-keys -X copy-selection

If you are using emacs copy mode, replacing copy-mode-vi with copy-mode should be enough.

like image 34
p1100i Avatar answered Oct 26 '22 20:10

p1100i