I'd like to automate the following task in tmux:

  • enter copy mode
  • move the cursor up X lines
  • select Y lines above the cursor
  • yank those lines

Is this feasible? How?

I was able to enter copy mode via :copy-mode but then I don't know how to proceed.

Many thanks.


I found it in the while.

Note: I have this option set in .tmux.conf that states that the keys hjkl should be used to move the cursor as in vim:

set-window-option -g mode-keys vi

I created a separate file which I named copy-that.tmux. It contains those lines:

send-keys kkkkkkkkkkkk
send-keys 0
send-keys v
send-keys jjjjjjjj
send-keys $
send-keys y

First line enters copy mode, the following move the cursor up. You can also use

send-keys Up

If you don't have vim-mode keys, but I wasn't able to find a way to put several of those in a single line. Then I move to the beginning of the line with '0', enter visual mode with 'v', go down with 'j', move to the end of the line with $, and finally yank the text with 'y'.

Then I bound the execution of the script in .tmux.conf: bind -n F6 run "/path/to/copy-that.tmux"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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