简体   繁体   English

从Vim中的脚本执行选择

[英]Execute selection from script in Vim

I'm trying to incorporate vim into my main workflow. 我正在尝试将vim纳入我的主要工作流程。 A major sticking point for me has been interactively editing and running programs/scripts. 对我来说,一个主要的关键点是交互式编辑和运行程序/脚本。

For example given that I'm currently vimmed into test.py 例如,鉴于我目前已进入test.py

print('hello')
x = 5
y = x+2
print(y)

Without leaving vim how would I: 不离开vim我怎么样:
a) run the whole script without leaving vim a)运行整个脚本而不离开vim
b) run just "print('hello')" b)只运行“print('hello')”

Commenters and the other answer have pointed out how to run a file from vim. 评论者和其他答案指出了如何从vim运行文件。 But they glossed over some really powerful possibilities. 但他们掩盖了一些非常强大的可能性。 I'd like to explain how some of those work in more detail. 我想解释其中一些如何更详细地工作。

The simplest possible way of running a python script in vim, is to just call the python interpreter on the file, eg 在vim中运行python脚本的最简单方法是在文件上调用python解释器,例如

:!python %

or, as I prefer to do to make sure there are no unsaved changes, 或者,我更喜欢确保没有未保存的更改,

:w | !python %

But it is not even necessary to have a file to run a python script in vim. 但是甚至没有必要让文件在vim中运行python脚本。 The reason why is because :w != save! 原因是因为:w !=保存! :w means write , and if no argument is provided, it happens to write to the file you are editing. :w表示写入 ,如果没有提供参数,则会写入正在编辑的文件。 However, you can write to STDOUT, to another file, or even to another program. 但是,您可以写入 STDOUT, 写入另一个文件,甚至写入另一个程序。 So if you'd like to run your buffer as python code without having a file to save and run, you may simply do: 因此,如果您希望将缓冲区作为python代码运行,而无需保存和运行文件,则可以执行以下操作:

:w !python

This meanse write the current buffer into the external program "python". 这meanse 写入当前缓冲区外部程序“蟒蛇”。 This literally just sends the contents of your buffer directly to python. 这实际上只是将缓冲区的内容直接发送到python。

Now here's where it gets really cool. 现在这里变得非常酷。 In vim, :w is an "ex command", eg a command that you run from the vim command line that originally came from ex , a very old line based unix text editor. 在vim中, :w是一个“ex命令”,例如你从最初来自ex的vim命令行运行的命令,这是一个非常古老的基于行的unix文本编辑器。 The awesome thing about ex commands is that since they are all line based, you can directly state which lines you would like the command to apply to. 关于ex命令的令人敬畏的事情是,由于它们都是基于行的,因此您可以直接说明要将命令应用于哪些行。 For example: 例如:

:2w myfile.txt

will write only line two to the file "myfile.txt". 将第二写入文件“myfile.txt”。 You can even supply a range, eg 你甚至可以提供一个范围,例如

:2,7w myfile.txt

will write lines 2-7 to "myfile.txt". 将第2-7行写入“myfile.txt”。 This means that using your example, we can run 这意味着使用您的示例,我们可以运行

:1w !python

to run just 刚刚运行

print('hello')

To make this more convenient, you can use visual mode to select every line you would like to run, which will automatically fill in the right range for you. 为了使这更方便,您可以使用可视模式选择您想要运行的每一行,这将自动为您填写正确的范围。 This will look like 这看起来像

:'<,'>w !python

To make this more convenient, I would recommend adding something like 为了使这更方便,我建议添加类似的东西

xnoremap <leader>p :w !python<cr>

to your .vimrc . 到你的.vimrc Then you can visually select whatever you want and run it as python code by typing 然后你可以直观地选择你想要的任何东西,并通过输入运行它作为python代码

\p

(replace \\ with whatever you have set up as your leader). (替换\\不管你已经设置为你的领导)。 You could also do 你也可以这样做

nnoremap <leader>p :w !python<cr>

or 要么

nnoremap <leader>p :w | !python %<cr>

depending on whether you want to save to a file or not. 取决于您是否要保存到文件。

Create a function for a range as discussed in this question: 问题中所述,为范围创建函数:

fu PyRun() range
  echo system('python -c ' . shellescape(join(getline(a:firstline, a:lastline), "\n")))
endf

Create a mapping for visual mode: 为可视模式创建映射:

vmap <C-F6> :call PyRun()<CR>

Then you can select a range and press Control - F6 . 然后您可以选择一个范围并按Control - F6 The range of lines will be executed by python . 行的范围将由python执行。 The result will be displayed in the command area. 结果将显示在命令区域中。

You can run a program from vim using :! 您可以使用:!从vim运行程序:! , ie :!python3 % to run your current script. ,即:!python3 %来运行您当前的脚本。

If you want to bind a key to it, another easy way would be to set makeprg to your python executable: :set makeprg=python3 and then bind a key to :make<cr> . 如果要将键绑定到它,另一种简单的方法是将makeprg设置为python可执行文件:set makeprg=python3然后将键绑定到:make<cr> In that case I would set up autocmds that switch the makeprg depending on the file type. 在这种情况下,我会设置makeprg根据文件类型切换makeprg

If you want to run a simple statement, you could either use Python's -c switch: 如果你想运行一个简单的语句,你可以使用Python的-c开关:

:!python3 -c 'print("Hello world")' , or you could just run :!python3 without arguments to be dropped into a REPL without leaving Vim. :!python3 -c 'print("Hello world")' ,或者你可以运行:!python3没有参数的:!python3被放入REPL而不离开Vim。

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

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