Send text to Tmux pane

1 Point

Enan Ajmain Enan Ajmain

5 years ago

This snippet will send selected text with :h v or send the whole paragraph the cursor is on. You need to provide the pane number of the pane you want to send the text to as count of the keybinding.

function! Send_to_tmux(visual, count) range abort
    let pane_index = system("tmux display-message -p '#{pane_index}'")
    if (pane_index == a:count)
        return
    endif
    let reg = @z
    if (a:visual)
        execute "normal! gv\"zy"
    else
        execute "normal! \"zyip"
    endif
    let text = @z
    let @z = reg
    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>