Linux 从 bash shell 脚本中打开 vim
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5978108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
open vim from within bash shell script
提问by webminal.org
I want bash shell that does the following
我想要执行以下操作的 bash shell
- open a file using vim
- write something into file
save and exit the file
bashpromt# cat mybash.sh echo "about to open a file" vim file.txt #I need to use vim application to open a file #now write something into file.txt #close the that file. echo "done."
- 使用 vim 打开文件
- 写一些东西到文件中
保存并退出文件
bashpromt# cat mybash.sh echo "about to open a file" vim file.txt #I need to use vim application to open a file #now write something into file.txt #close the that file. echo "done."
IS that possible? I found something like vim script , but not sure how to use it. Or something like "HERE" doc can be used for this ?
那可能吗?我找到了类似 vim script 的东西,但不知道如何使用它。或者像“HERE”文档之类的东西可以用于这个?
EDIT: I need to verify that vim editor is working fine over our file system.So I need to write script that invokes vim and does some command and close it. My requirement doesn't fit into doing stuffs like "echo "something" > file.txt" . I got to open the file using vim.
编辑:我需要验证 vim 编辑器在我们的文件系统上工作正常。所以我需要编写脚本来调用 vim 并执行一些命令并关闭它。我的要求不适合做诸如 "echo "something" > file.txt" 之类的事情。我必须使用vim打开文件。
采纳答案by Benoit
Vim has several options:
Vim 有几个选项:
-c
=> pass ex commands. Example:vim myfile.txt -c 'wq'
to force the last line of a file to be newline terminated (unlessbinary
is set in some way by a script)-s
=> play a scriptout that was recorded with-W
. For example, if your file containsZZ
, thenvim myfile.txt -s the_file_containing_ZZ
will do the same as previously.
-c
=> 传递 ex 命令。示例:vim myfile.txt -c 'wq'
强制文件的最后一行以换行符终止(除非binary
由脚本以某种方式设置)-s
=> 播放用-W
. 例如,如果您的文件包含ZZ
,vim myfile.txt -s the_file_containing_ZZ
则将执行与以前相同的操作。
Also note that, invoked as ex
, vim will start in ex mode ; you can try ex my_file.txt <<< wq
另请注意,调用 as 时ex
,vim 将以 ex 模式启动;你可以试试ex my_file.txt <<< wq
回答by Konerak
ex
is the commandline version for vi
, and much easier to use in scripts.
ex
是 的命令行版本vi
,在脚本中更容易使用。
ex $yourfile <<EOEX
:%s/$string_to_replace/$string_to_replace_it_with/g
:x
EOEX
回答by Sam Brinck
If you are wanting to see the work being done inside vim or gvim you can use --remote-send
如果您想查看在 vim 或 gvim 中完成的工作,您可以使用 --remote-send
gvim --servername SHELL_DRIVER
bashpromt# cat mybash.sh
#!/bin/bash
echo "about to open "
gvim --servername SHELL_DRIVER #I need to use vim application to open a file
#now write something into file.txt and close it
gvim --servername SHELL_DRIVER --remote-send '<ESC>i something to the file<ESC>:wq<CR>'
echo "done."
This will be slow but will do what you want it to.
First we open a gvim in which we can open all of our files (for efficiency)
With the first gvim line we open the file in the previously opened gvim.
On the second gvim line we send a command to the previously opened instance of gvim (with the desired file still open).
The command is as follows:<ESC>
- get out of any mode that gvim might have been ini something to the file
- go into insert mode and type " something to the file"<ESC>
- exit insert mode:wq
- write the file and quit vim
这会很慢,但会做你想做的。
首先我们打开一个 gvim,我们可以在其中打开我们所有的文件(为了效率)
使用第一行 gvim 我们在之前打开的 gvim 中打开文件。
在第二行 gvim 上,我们向先前打开的 gvim 实例发送一个命令(所需文件仍然打开)。
命令如下:<ESC>
- 退出 gvim 可能处于的任何模式i something to the file
- 进入插入模式并键入“something to the file” <ESC>
- 退出插入模式:wq
- 写入文件并退出 vim
回答by ib.
Recently I have answereda similar question, "automated edit of several files". May be solution that I describe there will satisfy your needs.
回答by Patrick Michaelsen
You asked how to write "something" into a text file via vim and no answer has necessarily covered that yet.
您询问了如何通过 vim 将“某物”写入文本文件,但目前还没有答案涵盖这一点。
To insert text:
插入文本:
ex $yourfile <<EOEX
:i
my text to insert
.
:x
EOEX
:i
enters insert mode. All following lines are inserted text until .
is seen appearing by itself on its own line.
:i
进入插入模式。以下所有行都是插入的文本,直到.
看到它自己出现在自己的行上。
Here is how to search and insert as well. You can do something such as:
以下是搜索和插入的方法。您可以执行以下操作:
ex $yourfile <<EOEX
:/my search query\zs
:a
my text to insert
.
:x
EOEX
This will find the first selection that matches regex specified by :/
, place the cursor at the location specified by \zs
, and enter insert mode after the cursor.
这将找到与 指定的正则表达式匹配的第一个选择:/
,将光标置于 指定的位置\zs
,并在光标后进入插入模式。
You can move \zs
to achieve different results. For example:
您可以移动\zs
以实现不同的结果。例如:
ex $yourfile <<EOEX
:/start of match \zs end of match
:a
my text to insert
.
:x
EOEX
This will change the first occurrence of "start of match end of match" to "start of match my text to insert end of match".
这会将第一次出现的“匹配开始结束”更改为“匹配开始我的文本以插入匹配结束”。
If you want to allow any amount of whitespace in your searches between keywords, use \_s*
. For example, searching for a function that returns 0: :/\_s*return\_s*0}
如果您想在关键字之间的搜索中允许任意数量的空格,请使用\_s*
. 例如,搜索返回 0 的函数::/\_s*return\_s*0}