如何通过shell脚本执行vim命令

时间:2022-06-05 01:16:21

Hi I have a situation as

嗨我有一个情况

I have a writing a shell script where I have to pass input to the vim.

我有一个shell脚本,我必须将输入传递给vim。

Explaining in detail

详细解释

Here I have already written shell script that I cannot changed. Here is the code for it.

在这里,我已经编写了我无法更改的shell脚本。这是它的代码。

 sudo vim /mnt/etc/{hosts,hostname,ports}

due to this hosts file is opened we can go manually next to the hostname file by :n and similar for the ports file.

由于打开了这个主机文件,我们可以通过以下方式手动在主机名文件旁边进行操作:n和ports文件类似。

But I have to perform same operation from my .sh file. Also I have to edit the ports file and after completing it I have to save and quite it through :wq command.

但是我必须从我的.sh文件中执行相同的操作。此外,我必须编辑ports文件,完成后我必须保存并完成它:wq命令。

How can I do it?

我该怎么做?

1 个解决方案

#1


6  

Alternatives

Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed, awk, or Perl / Python / Ruby / your favorite scripting language here.

除非你真的需要特殊的Vim功能,否则最好使用非交互式工具,如sed,awk或Perl / Python / Ruby /你最喜欢的脚本语言。

That said, you can use Vim non-interactively:

也就是说,您可以非交互式使用Vim:

Silent Batch Mode

For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute command), use Ex-mode.

对于非常简单的文本处理(即使用Vim,如增强的'sed'或'awk',可能只是受益于:substitute命令中的增强正则表达式),请使用Ex模式。

# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"

Attention: Vim will hang waiting for input if the "commands.ex" file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the - argument.

注意:如果“commands.ex”文件不存在,Vim将挂起等待输入;更好地检查它的存在!或者,Vim可以从stdin读取命令。您还可以使用从stdin读取的文本填充新缓冲区,如果使用 - 参数,则从stderr读取命令。

Full Automation

For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:

对于涉及多个窗口的更高级处理,以及Vim的真实自动化(您可以与用户交互或让Vim运行以让用户接管),请使用:

vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"

Here's a summary of the used arguments:

以下是使用的参数的摘要:

-T dumb           Avoids errors in case the terminal detection goes wrong.
-N -u NONE        Do not load vimrc and plugins, alternatively:
--noplugin        Do not load plugins.
-n                No swapfile.
-es               Ex mode + silent batch mode -s-ex
                Attention: Must be given in that order!
-S ...            Source script.
-c 'set nomore'   Suppress the more-prompt when the screen is filled
                with messages or output to avoid blocking.

#1


6  

Alternatives

Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed, awk, or Perl / Python / Ruby / your favorite scripting language here.

除非你真的需要特殊的Vim功能,否则最好使用非交互式工具,如sed,awk或Perl / Python / Ruby /你最喜欢的脚本语言。

That said, you can use Vim non-interactively:

也就是说,您可以非交互式使用Vim:

Silent Batch Mode

For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute command), use Ex-mode.

对于非常简单的文本处理(即使用Vim,如增强的'sed'或'awk',可能只是受益于:substitute命令中的增强正则表达式),请使用Ex模式。

# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"

Attention: Vim will hang waiting for input if the "commands.ex" file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the - argument.

注意:如果“commands.ex”文件不存在,Vim将挂起等待输入;更好地检查它的存在!或者,Vim可以从stdin读取命令。您还可以使用从stdin读取的文本填充新缓冲区,如果使用 - 参数,则从stderr读取命令。

Full Automation

For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:

对于涉及多个窗口的更高级处理,以及Vim的真实自动化(您可以与用户交互或让Vim运行以让用户接管),请使用:

vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"

Here's a summary of the used arguments:

以下是使用的参数的摘要:

-T dumb           Avoids errors in case the terminal detection goes wrong.
-N -u NONE        Do not load vimrc and plugins, alternatively:
--noplugin        Do not load plugins.
-n                No swapfile.
-es               Ex mode + silent batch mode -s-ex
                Attention: Must be given in that order!
-S ...            Source script.
-c 'set nomore'   Suppress the more-prompt when the screen is filled
                with messages or output to avoid blocking.