简体   繁体   English

当我尝试从我的 bash 脚本在后台运行 vim 时的奇怪行为

[英]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.

相关问题 当我尝试运行我的 bash 脚本时,它会向我抛出这样的错误 - When i try to run my bash script it throws me an error like this 在bash脚本中从/ dev / ttyS0读取时,结果很奇怪 - Weird result when reading from /dev/ttyS0 in a bash script 调用外部脚本时的奇怪行为 - weird behaviour when calling external script 在后台模式下运行 bash 脚本时的编码问题 - Encoding issue when run bash script in background mode 当外部客户端尝试在服务器上运行PHP脚本时,该怎么办? - What can I do when an external client try to run a PHP script on my server? 无法从 Bash 脚本在后台运行 Logstash - Can't Run Logstash in Background from Bash Script 当我尝试通过将命令添加到 /etc/profile 来在 Raspberry Pi 启动时执行它时,为什么我的 Python 脚本在后台运行两次? - Why is my Python script running twice in the background when I try to execute it at startup of my Raspberry Pi by adding the command to /etc/profile? 当我尝试运行从Windows系统编写的Linux脚本时出现“无此文件或目录”错误 - “No such file or directory” error when I try to run a Linux script written from Windows system 尝试运行此脚本时出现意外的文件结尾 - I get Unexpected End of File when i try and Run this Script 从脚本运行时,Bash脚本无法正确执行命令 - Bash script not executing command properly when run from script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM