[英]Weird behaviour when I try to run vim in background from my bash script
I need to open 20 files in vim using a loop, here is a loop I created for:我需要使用循环打开 vim 中的 20 个文件,这是我创建的循环:
for i in {1..20}
do
vim FILE$i &
done
After this, I'll expect that I have 20 jobs of vim run in the background.在此之后,我希望我有 20 个 vim 的作业在后台运行。 The actual result is that a random FILE from the loop ran in vim, and when I try to type something, the input does not display on the screen, and there is nothing I can do.
实际结果是循环中的随机 FILE 在 vim 中运行,当我尝试输入内容时,屏幕上不显示输入,我无能为力。 Sometimes it can be canceled by
Ctrl+C
.有时可以通过
Ctrl+C
取消。 So why it happens when I run my script, and how should I run 20 files in the background using vim?那么为什么在我运行脚本时会发生这种情况,我应该如何使用 vim 在后台运行 20 个文件?
Also when I try to run vim FILE1 &
in a terminal, all looks good to me, even when I run over 20 processes此外,当我尝试在终端中运行
vim FILE1 &
时,即使运行超过 20 个进程,对我来说一切都很好
Vim, at least the way you're invoking it, is a terminal-based program. Vim,至少您调用它的方式,是一个基于终端的程序。 That is, it puts the terminal into a suitable mode and writes data to it, usually including certain escape sequences, to draw on the screen and move the cursor in a certain way.
也就是说,它将终端置于合适的模式并向其写入数据,通常包括某些转义序列,以在屏幕上绘图并以某种方式移动 cursor。
The problem you've having is that you have one terminal and twenty instances of Vim.您遇到的问题是您有一个终端和二十个 Vim 实例。 Your instances of Vim are all expecting to control and monopolize the terminal and they are all fighting each other in this endeavor.
您的 Vim 实例都希望控制和垄断终端,并且它们都在为此而战。 As you might expect, this does not produce a useful result, and as such, you are probably not going to be happy with the result.
正如您所料,这不会产生有用的结果,因此,您可能不会对结果感到满意。 There is no way to avoid this, since this is generally how terminal-based programs operate and it will be true for all terminal-based editors.
没有办法避免这种情况,因为这通常是基于终端的程序的操作方式,并且对于所有基于终端的编辑器都是如此。
If you want to edit twenty different files in Vim, pass them all at once as arguments on the command line, and Vim will open all of them in one terminal.如果要编辑 Vim 中的 20 个不同文件,请在命令行中将它们作为 arguments 一次性传递,Vim 将在一个终端中打开所有这些文件。 That command should not be run in the background,.
该命令不应在后台运行。
If you want to run some Vimscript in a noninteractive way, put that into a file, say, script
, and then run vim -es '+source script' FILE$i
.如果您想以非交互方式运行一些 Vimscript,请将其放入文件中,例如
script
,然后运行vim -es '+source script' FILE$i
。 The -e
option puts Vim into ex mode where it runs only the commands you'd normally type starting with :
(the commands for the ex
editor) and this can be scripted in a noninteractive way with -s
. -e
选项将 Vim 置于 ex 模式下,它只运行您通常以:
开头的命令(用于ex
编辑器的命令),并且可以使用-s
以非交互方式编写脚本。 These commands can be run in the background if you redirect their output appropriately.如果您适当地重定向它们的 output,这些命令可以在后台运行。
If your goal was to do something else, you should open a new question asking how to do the thing you wanted to do.如果您的目标是做其他事情,您应该提出一个新问题,询问如何做您想做的事情。 We can then give you advice on how best to accomplish your goal in a helpful way.
然后,我们可以就如何以有益的方式最好地实现您的目标为您提供建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.