4

behave test language need to execute different lines of the behave feature file, for example, if I want to test the line number 99, I'll need to input the following line in the terminal

behave features/target.feature:99 -k

Currently I'm using Tmux and vim as my IDE. I have 2 tmux panes and A and B, vim editor is opened in pane A (as the editing/developing environment). pane B is simply a terminal used as the running the test

What I want' to achieve is: Get the current cursor-marked line number in the file open by vim in pane A(ex. 99) and send this line information to create a shell command: behave features/target.feature:99 -k and send this command to Tmux pane B to excut

So far I've tried

let runLine = line('.')
"autocmd FileType text nnoremap <space>r !sh -xc 'behave features/navmemory.feature:&runLine -k'

But the part for sending it to pane B and execute is missing and I don't know how can I do it

  • There are a number of vim plugins that specialize in sending commands to tmux; alternatively, your mapping can call a function that grabs the line number and runs tmux commands. Use that as a starting point to solve your problem – D. Ben Knoble Sep 5 '18 at 18:40
6

To send to another pane you'll need to call tmux from Vim. Specifically, you'll need the send-keys command (alias: send). For example, this will cause the current line number in Vim to be printed to the terminal in pane 2 using echo:

:exe "!tmux send -t 2 'echo " . line(".") . "' Enter"

Applying the same form to your command:

:exe "!tmux send -t 2 'behave features/target.feature:" . line(".") . " -k' Enter"

A few notes on -t:

  • The -t flag, in its simplest form, takes the target pane index as argument.
  • Since you are sending your command to another pane in the same window that's the only form we need to know.
  • You can display pane indexes in the current window with tmux-prefix + q.

FYI, that spelled-out Enter at the end of the command tells tmux to emit an actual carriage return at that point...submitting the command to the shell for execution, in these examples.

If you want to put this in a mapping the RHS is simply the full command from above followed by <CR>. So...

:MAPCMD LHS :exe "!tmux send -t 2 'behave features/target.feature:" . line(".") . " -k' Enter"<CR>

(I like to follow the <CR> with <C-L> to eliminate the need to hit Enter when the command is done.)

| improve this answer | |
2

Uploaded my personal config tvp-repl

Copy from one pane with Ctrl+c and paste to another (requires xclip)
vim with +clipboard, .vimrc:

set clipboard=unnamedplus
vnoremap <C-c> "+y

tmux bindings to dump the clipboard .tmux.conf:

bind-key Enter run "tmux send-keys -t 0 C-c" \; 
run "tmux select-pane -t 1" \; 
run "tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer" \; 
run "tmux send-keys -t 1 Enter" \; 
run "tmux select-pane -t 0"

Execution:

  • Enter in tmux will send CTRL+C to Pane 0 (vim).
    We bind CTRL+C in vim, to copy to the system clipboard.
  • We select Pane 1, where we want to paste the content.
  • Next the system clipboard will be extracted with xclip to the tmux buffer and immediately paste the buffer into the current selection (Pane 1).
  • Lastly, we send Enter and jump back to Pane 0 (vim).

The .tmux.conf is pretty human-readable. I personally call tmux with vim in 0 and ipython in 1 and prefer to keep it hard-coded. Alternatively, you could use run "tmux last-pane" to jump between the next and previous pane.

| improve this answer | |
  • It would be nice if you edited to explain how to use this better—I understand you say <C-c> to copy from one pane, but you only bind it for vim (so I'm not sure how you could do that in any tmux pane), and you don't mention the paste binding or how it works. Having glanced at that code, it appears it also won't work if you're not interested in exactly panes 0 and 1... – D. Ben Knoble Mar 3 at 15:36
  • @D.BenKnoble added the explanation. You are correct, that the solution is rather static w.r.t. the panes. My goal is to not remember another plugin such as vimux, which could be an alternative you want more flexibility. Probably you could also run CTRL+b q to display the pane numbering and select the target pane, which would introduce another keystroke. – dariohett Mar 5 at 23:52

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.