Monday, October 29, 2018

Re: Paste into a :term window?

On Thursday, October 25, 2018 at 2:33:38 PM UTC-5, Lifepillar wrote:
> On 25/10/2018 17:32, Kelvin Lawrence wrote:
> > I'm enjoying the new :term feature in 8.1
> >
> > I have my code on the left and a job running on the right. The job is a console environment for Groovy. Is there an easy way to copy from the code on the left and paste into the console I have running on the right? I know I cannot turn modifiable on but is there any way to send a paste command to the :term job?
>
> I use code similar to this:
>
> nnoremap <silent> <leader>x :<c-u>call TermSend([getline('.')])<cr>
> vnoremap <silent> <leader>x :<c-u>call TermSend(GetSelection())<cr>
>
> fun! TermSend(lines)
> if empty(get(b:, 'bound_terminal', '')) ||
> \ !bufexists(b:bound_terminal)
> let b:bound_terminal = str2nr(input('Terminal buffer: '))
> endif
> for l:line in a:lines
> call term_sendkeys(b:bound_terminal, l:line . "\r")
> endfor
> call cursor(line('.') + 1, 1) " Move to the next line
> endf
>
> fun! GetSelection()
> if getpos("'>") != [0, 0, 0, 0]
> let [lnum1, col1] = getpos("'<")[1:2]
> let [lnum2, col2] = getpos("'>")[1:2]
> let l:lines = getline(lnum1, lnum2)
> let l:lines[-1] = lines[-1][:col2-(&selection=='inclusive'?1:2)]
> let l:lines[0] = l:lines[0][col1 - 1:]
> return l:lines
> else
> return []
> end
> endf
>
> Then typing \x will send the current line or the current selection to
> a terminal buffer (the first time, you are prompted to choose the
> buffer's number). The function sends one line at a time: it is slower
> than sending the whole text in one swoop, but I have found it to be more
> reliable.
>
> Now, if only there was a simpler way to get the currently selected text…
>
> Enjoy,
> Life.

Thanks for the detailed reply. Very informative,
Cheers
Kelvin

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment