1

I am trying to send the vim line that is under the cursor to another tmux pane and then move the cursor on vim down 1 line.

For context, I have vim open on 1 pane, and python open on another. I want to bind this capability to a key so that I may execute my python code line by line through vim.

  • That sounds like a big XY problem. Why don't you simply use a built-in mechanism like :w with :.w !python for example? Which you can map easily to nnoremap <keytomap> :.w !python<CR> you could even add j at the end of the mapping to go the next line. – statox Apr 16 at 19:37
  • Thank you, however I do not want to run the entire python file. I want to run 1 line at a time. There is similar functionality in, for example, Spyder IDE, where only the current line under the cursor is sent to the python console. – showzen Apr 16 at 20:12
  • Related: vi.stackexchange.com/q/19209/10604 – D. Ben Knoble Apr 17 at 2:49
  • Well :.w !python doesn't execute the whole file it takes . as an address to send the current line to python. – statox Apr 17 at 7:17
1

The following function will send a paragraph of text or visually selected text to the tmux pane that corresponding to the count.

function! Send_to_tmux(visual, count) range abort
    if (a:visual)
        execute "normal! gv\"zy"
    else
        execute "normal! \"zyip"
    endif
    let text = @z
    let text = substitute(text, ';', '\\;', 'g')
    let text = substitute(text, '"', '\\"', 'g')
    let text = substitute(text, '\n', '" Enter "', 'g')
    let text = substitute(text, '!', '\\!', 'g')
    let text = substitute(text, '%', '\\%', 'g')
    let text = substitute(text, '#', '\\#', 'g')
    silent execute "!tmux send-keys -t " . a:count . " -- \"" . text . "\""
    silent execute "!tmux send-keys -t " . a:count . "Enter"
endfunction
nnoremap <Leader>p :<C-u>call Send_to_tmux(0, v:count1)<CR>
xnoremap <Leader>p :<C-u>call Send_to_tmux(1, v:count1)<CR>

For, example, you want to send a line to adjacent tmux pane and move down one line in vim, you can adjust the function somewhat like the following:

function! Send_to_tmux(visual, count) range abort
    execute "normal! vg_o^\"zy"
    let text = @z
    let text = substitute(text, ';', '\\;', 'g')
    let text = substitute(text, '"', '\\"', 'g')
    let text = substitute(text, '\n', '" Enter "', 'g')
    let text = substitute(text, '!', '\\!', 'g')
    let text = substitute(text, '%', '\\%', 'g')
    let text = substitute(text, '#', '\\#', 'g')
    silent execute "!tmux send-keys -t " . a:count . " -- \"" . text . "\""
    silent execute "!tmux send-keys -t " . a:count . "Enter"
    normal! j
endfunction
nnoremap <Leader>p :<C-u>call Send_to_tmux(0, v:count1)<CR>
xnoremap <Leader>p :<C-u>call Send_to_tmux(1, v:count1)<CR>

The above function will send the current line to the tmux pane corresponding to the count, i.e. if you press 2<Leader>p then the text will go the tmux pane number 2 and so on. Also, the cursor will go one line down.

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.