简体   繁体   English

如何在vim中将所有行输出到python控制台?

[英]how to output all the lines into python console in vim?

I have set F2 prompt key with map <f2> :w<cr>:! D:\\Python34\\python %<cr> 我已经用map <f2> :w<cr>:! D:\\Python34\\python %<cr>设置了F2提示键map <f2> :w<cr>:! D:\\Python34\\python %<cr> map <f2> :w<cr>:! D:\\Python34\\python %<cr> ,when i open an python file in vim and press F2,the python file will be executed .For a simple example, here is my python file and opened in gvim . map <f2> :w<cr>:! D:\\Python34\\python %<cr> ,当我在vim中打开python文件并按F2时,将执行python文件。举个简单的例子,这是我的python文件并在gvim中打开。

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

Now i can't input other python lines ,only thing i can do is to see the result and hit any key to close this window. 现在我无法输入其他python行,我唯一能做的就是查看结果并按任意键关闭此窗口。 What i want is : when i press F2, (the python file was opened in gvim) ,the python console pop up,and all the files in the python file were copied into the python console automatically,and i can go no to input some lines such as Obj().hello in the python console or go on to edit in gvim ,i am a lazy man ,the gvim and python console all opened waiting to serve me , can i write a vim scripts to achieve the target? 我想要的是:当我按F2,(python文件在gvim中打开)时,python控制台弹出,python文件中的所有文件都被自动复制到python控制台,我可以拒绝输入一些在python控制台中如Obj().hello这样的行或者继续在gvim中编辑,我是一个懒人,gvim和python控制台都打开等待为我服务,我可以编写一个vim脚本来实现目标吗? The command :!D:\\Python34\\python -i % works fine ,i got the ouput 命令:!D:\\Python34\\python -i %工作正常,我得到了输出 在此输入图像描述 There is still a problem remain, 1)when command :!D:\\Python34\\python -i % works ,the gvim window will be frozen , i can't drag my mouse to see codes in vim. 还有一个问题依旧,1)当命令:!D:\\Python34\\python -i %工作时,gvim窗口将被冻结,我无法拖动鼠标来查看vim中的代码。 2)there is no any python codes in the python console wiondow 2)python控制台wiondow中没有任何python代码

So if the program is full of many lines ,and i can't remember the previous content ,worse still, the gvim window frozen ,how can i get the codes? 因此,如果程序中有很多行,并且我记不起以前的内容,更糟糕的是,gvim窗口被冻结,我怎样才能获得代码? 在此输入图像描述

Avoid blocking 避免阻塞

To make the call asynchonous (to avoid that GVIM is blocked during the Python session), use the Windows-specific :!start command: 要使调用异步(以避免在Python会话期间阻止GVIM),请使用特定于Windows的:!start命令:

nnoremap <f2> :w<cr>:!start D:\Python34\python -i %<cr>

List teh codez 列出代码

I don't know whether it is possible to list the passed source code from the interactive Python debugger. 我不知道是否可以从交互式Python调试器列出传递的源代码。 But you can print the file contents before starting it: 但您可以在启动之前打印文件内容:

nnoremap <f2> :w<cr>:!start cmd /c type % && D:\Python34\python -i %<cr>

Additional tips 其他提示

  • You should use :noremap ; 你应该使用:noremap ; it makes the mapping immune to remapping and recursion. 它使映射不受重映射和递归的影响。
  • As your mapping only works correctly from normal mode, use :nnoremap (or extend it to support visual-mode selections, too). 由于您的映射仅在正常模式下正常工作,因此请使用:nnoremap (或将其扩展为支持可视模式选择)。

Maybe Vim plugin Conque will solve your problem: 也许Vim插件Conque将解决您的问题:

  1. Installation instrucions are here https://code.google.com/p/conque/ 安装说明在这里https://code.google.com/p/conque/
  2. To use just type :ConqueTermVSplit python -i test.py (VSplit is for vertical split - you may use horizontal) 只使用类型:ConqueTermVSplit python -i test.py (VSplit用于垂直拆分 - 你可以使用水平)
  3. There is no blocking of your window with python code - you may escape interactive mode and switch to your window with Ctrl+W twice 使用python代码没有阻止你的窗口 - 你可以逃避交互模式并用Ctrl + W两次切换到你的窗口

You could approach the problem from the Python angle (2.7). 您可以从Python角度(2.7)处理问题。

  1. Keep the file where it is (or save it with some unique name to a temporary directory) and have python load the file directly. 将文件保存在原始位置(或将其以一些唯一的名称保存到临时目录中)并让python直接加载文件。
  2. Go to that location in your shell and run python interactively (or have vim spin off an interpreter for you) 转到shell中的那个位置并以交互方式运行python(或让vim为你解析一个解释器)
  3. Import your file import demo 导入文件import demo
  4. Experiment with what you have implemented demo.SomeModule().meth() 试验你实现的demo.SomeModule().meth()
  5. Make some changes in vim 在vim中做一些改变
  6. Reload your python module reload(demo) 重新加载你的python模块reload(demo)
  7. Experiment with your code again demo.SomeModule().differentMeth() 再次试验你的代码demo.SomeModule().differentMeth()

You can also have vim create a file with shortcut functions for loading/reloading the file you are working on. 您还可以让vim创建一个带有快捷功能的文件,用于加载/重新加载您正在处理的文件。 When vim kicks off the interpreter, you can have it set this file to the PYTHONSTARTUP environment variable, which is a file the interpreter will automatically load when it starts up. 当vim启动解释器时,您可以将此文件设置为PYTHONSTARTUP环境变量,该变量是解释器在启动时自动加载的文件。 For example, you could have a function called r() to automatically reload the file you are working on. 例如,您可以使用一个名为r()的函数来自动重新加载您正在处理的文件。

It's also worth mentioning that reloading modules can be a little weird. 值得一提的是,重装模块可能有点奇怪。 If you instantiate some modules then reload the file, only new modules will use the new code; 如果你实例化一些模块然后重新加载文件,只有模块将使用新代码; the old modules will run with the old code. 旧模块将使用旧代码运行。

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

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