当脚本仍在运行时,如何使用 vim 从 Perl 编辑文本?
我有一个输出文本的 Perl 脚本。 我想将这段文本导入vim,编辑它,保存它,然后退出。 退出时,我希望原始 Perl 脚本处理编辑后的文件。
例如,当您添加新作业时,crontab -e
是如何工作的。
谢谢 :)
I have a Perl script that outputs text. I want to import this text into vim, edit it, save it and then exit. On exit, I want the original Perl script to process the edited file.
E.G. how crontab -e
works when you add a new job.
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来像是使用 system 在文件名上运行 vim 的非常简单的情况。 这将等到 vim 完成,此时您可以继续读取文件的新内容。
Sounds like a very plain case to use system to run vim on the filename. That will wait until vim is done, at which point you can go ahead and read the file's new contents.
建议:为什么不获取 crontab 的源代码并看看它做了什么?
除此之外,放松有你的答案。
我认为思考这个问题的方法是:
“好吧,我需要一个程序来写出一个文件,调用 vim,然后对文件进行进一步处理。所以,这是我编写文件的例程,这是我调用的例程vim(使用系统),这是我在 vim 完成后进行后处理的例程,现在我将它们全部放在我的主程序中,我就完成了!”
A suggestion: Why don't you grab the source to crontab and see what it does?
Other than that, unwind has your answer.
And I think the way to think about this is:
"Okay, I need a program to write out a file, call vim, then do further processing on the file. So, here's my routine to write the file, here's my routine that calls vim (using system), and here's my routine to do the postprocessing after vim is done. Now I'll lace them all together in my main program, and I'm done!"
您似乎想将脚本的输出通过管道传输到 vim 中:
然后您希望脚本以某种方式知道 Vim 已完成并恢复。 I/O 重定向不是这样工作的。 该脚本不知道其输出正在通过管道传输到哪个程序。
您提到了其他程序,例如 crontab 和 cvs; 它们的一个共同点是它们自己调用编辑器。 他们创建临时文件,读取 EDITOR 或 VISUAL 环境变量(有关如何选择哪一个的详细信息,请查看手册),运行给定的程序,然后等待程序完成。 然后他们继续运行并使用他们之前指定的文件。
事实证明我已经在 Perl 中做到了这一点。 我创建了一个临时文件(使用
tempfile
),在其中写入一堆文本,然后使用 < code>system 调用文件编辑器。 您甚至不必在运行编辑器时关闭文件。You seem to want to pipe the output of your script into vim:
And then you want the script to somehow know that Vim is finished and resume. That's not how I/O redirection works. The script has no knowledge of what program its output is being piped into.
You mentioned other programs like
crontab
andcvs
; one thing those have in common is that they invoke the editor themselves. They create temporary files, read the EDITOR or VISUAL environment variables (check the manual for details about how they choose which one), run the given program, and wait for the program to finish. Then they continue running and use the file they specified earlier.Turns out I've done exactly that in Perl. I created a temporary file (with
tempfile
), wrote a bunch of text into it, and then usedsystem
to invoke the editor on the file. You don't even have to close the file while you run the editor.将输出通过管道传输到 vim - 并且不要忘记末尾的破折号。
这将打开 vim,并显示脚本输出的内容。 只需保存:w 文件名 并退出:q。 这不是 crontab -e 的工作方式,但它以一种简单的方式足够接近。
编辑:
我错过了这一部分(或者问题可能是后来被编辑的):
这不是最好的方法,但按照我原来的答案,这是可行的(如果您将文件另存为 file.txt):
只需将 perl 命令替换为您的 perl 命令,它就应该可以工作。 就像我说的,这不是最好的答案,但它应该有效。
Pipe the output to vim - and don't forget the dash at the end.
This will open vim with the contents of the scripts output. Just save it :w filename and exit :q. This isn't how crontab -e works, but it comes close enough in an easy way.
Edit:
I missed this part (or maybe the question was edited afterward):
This isn't the best way to do this, but going with my original answer, this works (if you save the file as file.txt):
Just replace the perl commands with your perl commands and it should work. Like I said, this is not the best answer, but it should work.