4

I have an open ipython terminal open, and I'd like to paste the visual selection to it, with prepending "%cpaste" and appending "--"

This is what I came up with

function! VisualSelection()
    if mode()=="v"
        let [line_start, column_start] = getpos("v")[1:2]
        let [line_end, column_end] = getpos(".")[1:2]
    else
        let [line_start, column_start] = getpos("'<")[1:2]
        let [line_end, column_end] = getpos("'>")[1:2]
    end
    if (line2byte(line_start)+column_start) > (line2byte(line_end)+column_end)
        let [line_start, column_start, line_end, column_end] =
        \   [line_end, column_end, line_start, column_start]
    end
    let lines = getline(line_start, line_end)
    if len(lines) == 0
            return get_line(line('.'))                                                                                                                                                                               endif
    let lines[-1] = lines[-1][: column_end - 1]
    let lines[0] = lines[0][column_start - 1:]
    return join(lines, "\n")
endfunction

vnoremap <silent> <F7> :call term_sendkeys(term_list()[0], "%cpaste\n".VisualSelection())."\n--\n")<cr>

Unfortunately, for some reason the string "cpaste" gets sent multiple times, and the rest is sent after that.

Any ideas what could be the problem ?

2
  • I don't know about the prepending and appending, but the vim-slime plugin allows you to send some text from vim (e.g. a visual selection, or using normal mode motions) to a target (like python)
    – mattb
    Commented Nov 28, 2021 at 0:26
  • A different approach might be to use tmux and a custom key-binding in copy-mode.
    – Rolf
    Commented Dec 3, 2021 at 8:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.