在 Vim 中运行 Python 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18948491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:22:31  来源:igfitidea点击:

Running Python code in Vim

pythonvim

提问by multigoodverse

I am writing Python code using Vim, and every time I want to run my code, I type this inside Vim:

我正在使用 Vim 编写 Python 代码,每次我想运行我的代码时,我都会在 Vim 中输入:

:w !python

This gets frustrating, so I was looking for a quicker method to run Python code inside Vim. Executing Python scripts from a terminal maybe? I am using Linux.

这令人沮丧,所以我正在寻找一种更快的方法来在 Vim 中运行 Python 代码。也许从终端执行 Python 脚本?我正在使用 Linux。

采纳答案by Kent

How about adding an autocmdto your ~/.vimrc-file, creating a mapping:

如何autocmd向您的~/.vimrc-file添加一个,创建一个映射:

autocmd FileType python map <buffer> <F9> :w<CR>:exec '!python3' shellescape(@%, 1)<CR>
autocmd FileType python imap <buffer> <F9> <esc>:w<CR>:exec '!python3' shellescape(@%, 1)<CR>

then you could press <F9>to execute the current buffer with python

然后你可以按下<F9>执行当前缓冲区python

Explanation:

解释:

  • autocmd: command that Vim will execute automatically on {event}(here: if you open a python file)
  • [i]map: creates a keyboard shortcut to <F9>in insert/normal mode
  • <buffer>: If multiple buffers/files are open: just use the active one
  • <esc>: leaving insert mode
  • :w<CR>: saves your file
  • !: runs the following command in your shell (try :!ls)
  • %: is replaced by the filename of your active buffer. Butsince it can contain things like whitespace and other "bad" stuff it is better practise not to write :python %, but use:
  • shellescape: escape the special characters. The 1means with a backslash
  • autocmd: Vim 将自动执行的命令{event}(这里:如果你打开一个 python 文件)
  • [i]map:<F9>在插入/正常模式下创建一个键盘快捷键
  • <buffer>:如果多个缓冲区/文件打开:只使用活动的
  • <esc>: 退出插入模式
  • :w<CR>: 保存你的文件
  • !: 在你的 shell 中运行以下命令(尝试:!ls
  • %: 由活动缓冲区的文件名替换。但是由于它可以包含诸如空格和其他“坏”东西之类的东西,因此最好不要写:python %,而是使用:
  • shellescape: 转义特殊字符。1带反斜杠的意思

TL;DR: The first line will work in normal mode and once you press <F9>it first saves your file and then run the file with python. The second does the same thing, but leaves insert mode first

TL;DR:第一行将在正常模式下工作,一旦你按下<F9>它,首先保存你的文件,然后用 python 运行文件。第二个做同样的事情,但首先离开插入模式

回答by TankorSmash

Keep in mind that you're able to repeat the last used command with @:, so that's all you'd need to repeat are those two character.

请记住,您可以使用 重复上次使用的命令@:,因此您只需要重复这两个字符即可。

Or you could save the string w !pythoninto one of the registers (like "afor example) and then hit :<C-R>a<CR>to insert the contents of register ainto the commandline and run it.

或者您可以将字符串保存w !python到其中一个寄存器中("a例如),然后点击:<C-R>a<CR>将寄存器的内容插入a命令行并运行它。

Or you can do what I do and map <leader>zto :!python %<CR>to run the current file.

或者你可以做我想做的,并映射<leader>z:!python %<CR>运行当前文件。

回答by multigoodverse

A simple method would be to type :while in normal mode, and then press the up arrow key on the keyboard and press Enter. This will repeat the last typed commands on VIM.

一种简单的方法是:在正常模式下键入,然后按键盘上的向上箭头键并按 Enter。这将在 VIM 上重复上次键入的命令。

回答by shmup

If you want to quickly jump back through your :wcommands, a cool thing is to type :wand then press your up arrow. It will only cycle through commands that start with w.

如果您想快速跳回您的:w命令,一个很酷的方法是键入:w然后按向上箭头。它只会循环以w.开头的命令。

回答by czayas

I have this in my .vimrc file:

我的 .vimrc 文件中有这个:

imap <F5> <Esc>:w<CR>:!clear;python %<CR>

When I'm done editing a Python script, I just press <F5>. The script is saved and then executed in a blank screen.

完成 Python 脚本的编辑后,只需按<F5>。脚本被保存,然后在空白屏幕中执行。

回答by Bob Cressman

Put your cursor in the code somewhere. Right click and choose one of the "Select" choices to highlight your code. Then press Ctrl : and you will see the new prompt '<, >'

将光标放在代码中的某处。右键单击并选择“选择”选项之一以突出显示您的代码。然后按 Ctrl : 你会看到新的提示 '<, >'

Now type !python and see if that works.

现在输入 !python 并查看是否有效。

I just spend days trying to figure out the same problem!!! I used the coding:

我只是花了几天时间试图找出同样的问题!!!我使用了编码:

s='My name'
print (s) 

After I pulled out all my hair, I finally got it right!

在我拔掉所有头发后,我终于做对了!

回答by user2587717

This .vimrc mapping needs Conque Shell, but it replicates Geany (and other X editors') behaviour:

这个 .vimrc 映射需要Conque Shell,但它复制了 Geany(和其他 X 编辑器)的行为:

  • Press a key to execute
  • Executes in gnome-terminal
  • Waits for confirmation to exit
  • Window closes automatically on exit

    :let dummy = conque_term#subprocess('gnome-terminal -e "bash -c \"python ' . expand("%") . '; answer=\\\"z\\\"; while [ $answer != \\\"q\\\" ]; do printf \\\"\nexited with code $?, press (q) to quit: \\\"; read -n 1 answer; done; \" "')

  • 按一个键执行
  • 在 gnome-terminal 中执行
  • 等待确认退出
  • 退出时窗口自动关闭

    :let dummy = conque_term#subprocess('gnome-terminal -e "bash -c \"python ' . expand("%") . '; answer=\\\"z\\\"; while [ $answer != \\\"q\\\" ]; do printf \\\"\nexited with code $?, press (q) to quit: \\\"; read -n 1 answer; done; \" "')

回答by Yosh

For generic use (run python/haskell/ruby/C++... from vim based on the filetype), there's a nice plugin called vim-quickrun. It supports many programming languages by default. It is easily configurable, too, so one can define preferred behaviours for any filetype if needed. The github repo does not have a fancy readme, but it is well documented with the doc file.

对于一般用途(运行 python/haskell/ruby/C++... 从基于 的vim filetype),有一个很好的插件叫做vim-quickrun。它默认支持多种编程语言。它也很容易配置,因此可以根据需要为任何文件类型定义首选行为。github repo 没有花哨的自述文件,但它在 doc 文件中有很好的记录。

回答by derwin

I have this on my .vimrc:

我的 .vimrc 上有这个:

"map <F9> :w<CR>:!python %<CR>"

which saves the current buffer and execute the code with presing only Esc + F9

保存当前缓冲区并仅按 Esc + F9 执行代码

回答by Jabba

If you don't want to see ":exec python file.py" printed each time, use this:

如果您不想:exec python file.py每次都看到 " " 打印,请使用以下命令:

nnoremap <F9> :echo system('python2 "' . expand('%') . '"')<cr>
nnoremap <F10> :echo system('python3 "' . expand('%') . '"')<cr>

It didn't mess up my powerline / vim-airline statusbar either.

它也没有弄乱我的 powerline / vim-airline 状态栏。