在 Vim 中从脚本执行选择 [英] Execute selection from script in Vim

查看:20
本文介绍了在 Vim 中从脚本执行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 vim 合并到我的主要工作流程中.我的一个主要症结是交互式编辑和运行程序/脚本.

I'm trying to incorporate vim into my main workflow. A major sticking point for me has been interactively editing and running programs/scripts.

例如,我目前正在进入 test.py

For example given that I'm currently vimmed into test.py

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

不离开vim我会怎样:
a) 在不离开 vim 的情况下运行整个脚本
b) 只运行print('hello')"

Without leaving vim how would I:
a) run the whole script without leaving vim
b) run just "print('hello')"

推荐答案

评论者和其他答案指出了如何从 vim 运行文件.但他们掩盖了一些非常强大的可能性.我想更详细地解释其中一些是如何工作的.

Commenters and the other answer have pointed out how to run a file from vim. But they glossed over some really powerful possibilities. I'd like to explain how some of those work in more detail.

在 vim 中运行 python 脚本的最简单可能的方法是在文件上调用 python 解释器,例如

The simplest possible way of running a python script in vim, is to just call the python interpreter on the file, e.g.

:!python %

或者,我更喜欢确保没有未保存的更改,

or, as I prefer to do to make sure there are no unsaved changes,

:w | !python %

但是在vim中运行python脚本甚至不需要文件.原因是因为 :w != save! :w 的意思是write,如果没有提供参数,它恰好写入您正在编辑的文件.但是,您可以写入到 STDOUT、另一个文件,甚至另一个程序.因此,如果您想将缓冲区作为 python 代码运行,而无需保存和运行文件,您可以简单地执行以下操作:

But it is not even necessary to have a file to run a python script in vim. The reason why is because :w != save! :w means write, and if no argument is provided, it happens to write to the file you are editing. However, you can write to STDOUT, to another file, or even to another program. So if you'd like to run your buffer as python code without having a file to save and run, you may simply do:

:w !python

这意味着写入当前缓冲区外部程序python".这实际上只是将缓冲区的内容直接发送到 python.

This meanse write the current buffer into the external program "python". This literally just sends the contents of your buffer directly to python.

现在它变得非常酷.在 vim 中,:w 是一个ex 命令",例如从 vim 命令行运行的命令,最初来自 ex,一个非常基于旧行的 Unix 文本编辑器.ex 命令的惊人之处在于,由于它们都是基于行的,因此您可以直接说明您希望该命令应用于哪些行.例如:

Now here's where it gets really cool. In vim, :w is an "ex command", e.g. a command that you run from the vim command line that originally came from ex, a very old line based unix text editor. 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. For example:

:2w myfile.txt

只写第二行到文件myfile.txt".您甚至可以提供一个范围,例如

will write only line two to the file "myfile.txt". You can even supply a range, e.g.

:2,7w myfile.txt

将第 2-7 行写入myfile.txt".这意味着使用您的示例,我们可以运行

will write lines 2-7 to "myfile.txt". This means that using your example, we can run

:1w !python

运行只是

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>

到你的 .vimrc.然后您可以直观地选择您想要的任何内容并通过键入

to your .vimrc. Then you can visually select whatever you want and run it as python code by typing

\p

(将 \ 替换为您已设置为领导者的任何内容).你也可以这样做

(replace \ with whatever you have set up as your leader). You could also do

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

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

取决于您是否要保存到文件.

depending on whether you want to save to a file or not.

这篇关于在 Vim 中从脚本执行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆