简体   繁体   English

如何创建映射以执行Vim中的python代码?

[英]How to create a mapping to execute python code from Vim?

Here's the steps I follow to execute python code from Vim: 这是我从Vim执行python代码所遵循的步骤:

  1. I write a python source code using Vim 我使用Vim编写了python源代码
  2. I execute it via :w !python 我通过:w !python执行它

and I'd like a shorter way to execute the code, so I searched the Internet for a shortcut key such as this map: 并且我想要一种更短的代码执行方式,因此我在互联网上搜索了如下图所示的快捷键:

autocmd FileType python nnoremap <buffer> <F9> :exec '!python' shellescape(@%, 1)<cr>

and added it to the _vimrc file. 并将其添加到_vimrc文件中。 But when I hit F9 , the following error message appears: 但是当我按下F9时 ,会出现以下错误消息:

在此处输入图片说明

Here is the relevant text in the image above: 这是上图中的相关文本:

python: can't open file ''D:/py2.py'': [Errno 22] Invalid argument
shell returned 2.

I have already searched the Internet, but I have not got any result. 我已经搜索了Internet,但是没有任何结果。

I use gVim 8.0 in Windows 10, if that helps. 我可以在Windows 10中使用gVim 8.0,如果有帮助的话。

Interacting with command-line through vim, the much preferred way would be with exclamation. 通过vim与命令行交互时,更可取的方式是使用感叹号。
Instead of: 代替:

:exec '!python' shellescape(@%, 1)<cr>

Try doing the easy way: 尝试做一个简单的方法:

:update<bar>!python %<CR>

here in the above code: 在上面的代码中:
update save file if modified ( preferred ) or can also use w . 如果已修改( 首选 ),则update保存文件,或者也可以使用w
<bar> pipe commands. <bar>管道命令。
!python % will run the file as python filename . !python %将以python filename运行python filename

So basically, put this in your .vimrc file, and that'll do it: 因此,基本上,将其放在您的.vimrc文件中,即可完成操作:

autocmd FileType python nnoremap <buffer> <F9> :update<bar>!python %<CR>

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

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