3

I am using the latest vim version.

vim --version | head -1
VIM - Vi IMproved 8.1 (2018 May 18, compiled Aug 12 2019 17:28:55)

Edit a python file with vim.

vim  embed.py
x = 3 
print(x)
y =4 
print(x+y)

Now open a new window with ter command in vim. enter image description here

The normal way to execute embed.py which is in edit status.

:! python3 %    

enter image description here

New window open and execute embed.py.
enter image description here

I have a new idea,how can copy all the lines in embed.py into the above window opened by ter command in vim?Show the expected way as below.

enter image description here ggyG can't work. Move cursor in vim window,and press ggyG. enter image description here Move cursor in the python3 window.
ctrl + v can't work, <C-\><C-N> can't work too.
It is time to try with gui way,paste nothing also.

enter image description here enter image description here

Do as Tarun Lalwani say:
step1: copy lines into system clipboard

:%y+

or with other command.

step2: move cursor into the upper window which run python3.
step3: ctrl+v+shift

How can bind all steps with a hot key?
Status 1:

Write the following in my .vimrc.

function! CopyPasteBuffer()
     normal gg"+yG
     wincmd p
     call feedkeys('^W"+')
endfunction

nnoremap <leader>p :call CopyPasteBuffer()<CR>

\p will put ^W"+ on python3's interactive window. enter image description here

Status 2:

Write the following in my .vimrc.

function! CopyPasteBuffer()
     normal gg"+yG
     wincmd p
endfunction

nnoremap <leader>p :call CopyPasteBuffer()<CR>

\p will move cursor into upper window,now pressing ctrl+v+sfift can take effect.
enter image description here

Almost done!It remains a issue here.
The last step (step 3) which paste all program's lines into python interactive window haven't been automated into the vimscript,rkta's CopyPasteBuffer() only bind two steps with hot key \p successfully.
Please have a try in bash ,instead of zsh. Almost same result both for normal gg"+yG and normal gg"*yG,ctrl+v+shift or ctrl+w+ctrl+v or ctrl+v can't paste content in register * if it is normal gg"*yG in CopyPasteBuffer()(verified in my bash).

showkey
  • 482
  • 42
  • 140
  • 295
  • As usual, yank (`ggyG`) from one window, paste (`p`) to the other. (You will need to be in normal mode in the terminal window, you can use `` to get there if needed.) – Amadan Aug 22 '19 at 07:14
  • The most important work is to bind all steps with a hot key, system clipboard or not does not matter. – showkey Aug 25 '19 at 08:53
  • How to express move cursor into the upper window?I am trying to write a binding. – showkey Aug 25 '19 at 08:54

4 Answers4

5

There is a built-in function named term_sendkeys to send keys to a terminal buffer.

Here is a oneliner to send all lines in the current buffer to the first terminal window using term_sendkeys:

:cal term_sendkeys(term_list()[0], join(map(getbufline(bufnr('.'), 1, '$'), 'v:val . "\n"'), ''))

You can simply define a map to execute the oneliner in your .vimrc like this:

nnoremap <leader>p :<c-u>cal term_sendkeys(term_list()[0], join(map(getbufline(bufnr('.'), 1, '$'), 'v:val . "\n"'), ''))<Cr>

However oneliners are bit hard to understand at glance, so it is better to define this as a function and define a map to call it:

function! s:SendLinesToTerm()
  let term_buf = term_list()[0]
  let lines = getbufline(bufnr('.'), 1, '$')
  let str = join(map(lines, 'v:val . "\n"'), '')

  cal term_sendkeys(term_buf, str)
endfunction
nnoremap <leader>p :call <SID>SendLinesToTerm()<Cr>
Tacahiroy
  • 643
  • 4
  • 16
3

You have to understand that when you yank the lines in vim it is not basically going to system's clipboard. The terminal shown in the upper window can only interact with the system's clipboard

You can see the below thread on how to use system clipboard

https://vi.stackexchange.com/questions/84/how-can-i-copy-text-to-the-system-clipboard-from-vim

I use mac which has pbcopy to copy to the clipboard. So I can execute something like :silent !pbcopy < %. This will copy the file to clipboard. And then a normal CTRL+V or CTRL+SHIFT+V or CMD+V would work based on your OS

For unix you would use something like xclip

Working

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
3

To copy the current buffer, switch to the terminal running in the only split and paste the buffer contents use this function:

function! CopyPasteBuffer()
     normal ggyG
     wincmd p
     call feedkeys("\<C-W>\"*")
endfunction

(As we are in terminal mode, we need to use Ctrl W " to paste, see :h terminal-typing for other special keys.)

This will paste everything and leave you in the terminal buffer - use Ctrl WW to switch back.

To bind it to a key use

nnoremap <leader>p :call CopyPasteBuffer()<CR>

If you didn't rebind your leader key you can execute the function with \p.


To use the function with the * register just change the function to

function! CopyPasteBuffer()
     normal gg"*yG
     wincmd p
     call feedkeys("\<C-W>\"*")
endfunction

enter image description here

rkta
  • 3,959
  • 7
  • 25
  • 37
  • `"*p` means paster, what dows `` in `call feedkeys("\\"*")` mean?Is there a paste action in `call feedkeys("\\"*")`? – showkey Aug 28 '19 at 07:10
0

How to use the terminal window

According to this SO answer and @Amadan’s comment, in a terminal window, the command ctrl-wN (capital N) allows to exit the “insertion mode” (so that you can copy things from the terminal window); also, ctrl-w" followed by the appropriate register name (* for X primary, + for X clipboard) allows to paste the contents of the said register (that’s what you are interested in). You may also paste the primary register with ctrl-insert, and the clipboard register with the window menu you show on one of your screenshots.

How to use registers

On registers: long story short, Vim stores yanked text in various named registers. If you are running Vim from an X graphical environment, the Vim register * is connected to the X clipboard “primary” (usually the last text selected with the mouse from within a graphical application), and the Vim register + is connected to the X clipboard “clipboard” (usually the last text copied with the shortcut ctrl-v from within a graphical application). By default, Vim puts text yanked with the command yy in the register *, but you can put it in + as well, and you can change the default to + (set clipboard=unnamedplus).

Use whichever register you prefer. Only make sure you use the same when copying and pasting (i.e., by default yy will typically copy to X primary while the window menu will paste from the X clipboard).

Official source: Read :help terminal to learn how to use Vim’s terminal windows, and :help registers for Vim’s registers.

(Credible source: Google PageRank.)


However in your case, aren’t you looking rather at the Python keyword for importing a file into the REPL? Although I don’t know Python very well, it should probably looks like import embed or something.

Maëlan
  • 3,586
  • 1
  • 15
  • 35
  • Dont need import; OP wants to use a sort of interactive repl, with a send-line-to-repl or send-file-to-repl command (a bit like slime, if youve ever used it for lisp). – D. Ben Knoble Aug 24 '19 at 18:38