简体   繁体   中英

Copy lines in vim to execute in above window opened by ter command?

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. 在此输入图像描述

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

:! python3 %    

在此输入图像描述

New window open and execute embed.py .
在此输入图像描述

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.

在此输入图像描述 ggyG can't work. Move cursor in vim window,and press ggyG . 在此输入图像描述 Move cursor in the python3 window.
ctrl + v can't work, <C-\\><CN> can't work too.
It is time to try with gui way,paste nothing also.

在此输入图像描述 在此输入图像描述

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. 在此输入图像描述

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.
在此输入图像描述

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).

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>

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

工作

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 W W 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

在此输入图像描述

How to use the terminal window

According to this SO answer and @Amadan's comment, in a terminal window, the command ctrl-w N (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 y y 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 ( ie , by default y y 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM