简体   繁体   English

复制vim中的行以在ter命令打开的上面窗口中执行?

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

I am using the latest vim version. 我使用的是最新的vim版本。

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编辑python文件。

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

Now open a new window with ter command in vim. 现在用vim中的ter命令打开一个新窗口。 在此输入图像描述

The normal way to execute embed.py which is in edit status. 执行处于编辑状态的embed.py的常规方法。

:! python3 %    

在此输入图像描述

New window open and execute embed.py . 打开新窗口并执行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. 我有一个新的想法,如何将embed.py所有行复制到vim中由ter命令打开的上面窗口中?显示预期的方式如下。

在此输入图像描述 ggyG can't work. ggyG无法工作。 Move cursor in vim window,and press ggyG . 将光标移动到vim窗口,然后按ggyG 在此输入图像描述 Move cursor in the python3 window. 在python3窗口中移动光标。
ctrl + v can't work, <C-\\><CN> can't work too. ctrl + v无法正常工作, <C-\\><CN>也无法正常工作。
It is time to try with gui way,paste nothing also. 现在是时候尝试gui方式,也没有粘贴。

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

Do as Tarun Lalwani say: 如Tarun Lalwani所说:
step1: copy lines into system clipboard step1:将行复制到系统剪贴板中

:%y+

or with other command. 或与其他命令。

step2: move cursor into the upper window which run python3. step2:将光标移动到运行python3的上层窗口。
step3: ctrl+v+shift step3:ctrl + v + shift

How can bind all steps with a hot key? 如何使用热键绑定所有步骤?
Status 1: 状态1:

Write the following in my .vimrc. 在我的.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. \\p将把^W"+放在python3的交互式窗口上。 在此输入图像描述

Status 2: 状态2:

Write the following in my .vimrc. 在我的.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. \\p将光标移动到上方窗口,现在按ctrl+v+sfift即可生效。
在此输入图像描述

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. 将所有程序的行粘贴到python交互式窗口中的最后一步(步骤3)尚未自动进入vimscript,rkta的CopyPasteBuffer()仅使用热键\\p成功绑定两个步骤。
Please have a try in bash ,instead of zsh. 请试试bash,而不是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). 对于normal gg"+yGnormal gg"*yGctrl+v+shiftctrl+w+ctrl+vctrl+v几乎相同的结果都不能在寄存器中粘贴内容*如果它是normal gg"*yGCopyPasteBuffer() (在我的bash中验证)。

There is a built-in function named term_sendkeys to send keys to a terminal buffer. 有一个名为term_sendkeys的内置函数可将密钥发送到终端缓冲区。

Here is a oneliner to send all lines in the current buffer to the first terminal window using term_sendkeys : 这是一个使用term_sendkeys将当前缓冲区中的所有行发送到第一个终端窗口的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: 您可以简单地定义一个映射来执行.vimrc的oneliner,如下所示:

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: 但是oneliner一眼就难以理解,因此最好将其定义为函数并定义一个映射来调用它:

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. 你必须明白,当你在vim划线时,它基本上不会进入系统的剪贴板。 The terminal shown in the upper window can only interact with the system's clipboard 上部窗口中显示的terminal只能与系统的剪贴板进行交互

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 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. 我使用具有pbcopy mac复制到剪贴板。 So I can execute something like :silent !pbcopy < % . 所以我可以执行类似: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 然后正常的CTRL+VCTRL+SHIFT+VCMD+V将根据您的操作系统工作

For unix you would use something like xclip 对于unix,你会使用像xclip这样的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.) (因为我们处于终端模式,我们需要使用Ctrl W“粘贴,请参阅:h terminal-typing for other other keys。)

This will paste everything and leave you in the terminal buffer - use Ctrl W W to switch back. 这将粘贴所有内容并将您留在终端缓冲区中 - 使用Ctrl W W切换回来。

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 . 如果您没有重新绑定leader密钥,则可以使用\\ 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); 根据这个SO答案和@Amadan的评论,在终端窗口中,命令ctrl -w N (大写N)允许退出“插入模式”(这样你就可以从终端窗口复制东西); 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. 另外, ctrl-w 后跟相应的寄存器名称( *表示X primary, +表示X剪贴板)允许粘贴所述寄存器的内容(这是你感兴趣的内容)。你也可以用ctrl粘贴主寄存器-insert ,剪贴板注册您在其中一个屏幕截图上显示的窗口菜单。

How to use registers 如何使用寄存器

On registers: long story short, Vim stores yanked text in various named registers . 关于寄存器:长话短说,Vim存储了各种命名寄存器中的文本。 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). 如果从X图形环境运行Vim,则Vim寄存器*连接到X剪贴板“primary”(通常是使用鼠标从图形应用程序中选择的最后一个文本),并且Vim寄存器+连接到X剪贴板“剪贴板”(通常是在图形应用程序中使用快捷键ctrl-v复制的最后一个文本)。 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 ). 默认情况下,Vim将文本与命令y y一起放入寄存器* ,但您也可以将其放在+中,并且可以将默认值更改为+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). 只有确保复制和粘贴( 默认为Y Y通常会复制到X主,而窗口菜单将从X剪贴板粘贴),当您使用相同的。

Official source: Read :help terminal to learn how to use Vim's terminal windows, and :help registers for Vim's registers. 官方消息来源:阅读:help terminal学习如何使用Vim的终端窗口,以及:help registers Vim的寄存器。

( Credible source: Google PageRank.) 可信来源:谷歌PageRank。)


However in your case, aren't you looking rather at the Python keyword for importing a file into the REPL? 但是在你的情况下,你不是在寻找用于将文件导入REPL的Python关键字吗? Although I don't know Python very well, it should probably looks like import embed or something. 虽然我不太了解Python,但它应该看起来像import embed或者其他东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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